Very frustrating problem with JList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wishbone34
    New Member
    • Mar 2007
    • 10

    Very frustrating problem with JList

    Hi, here is some of my code

    public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

    private JList Items;

    //builds the UI
    public ControlIL(){

    DefaultListMode l model = new DefaultListMode l();
    JList Items = new JList(model);
    }



    I want to be able to add to the List when a certain button I have is pressed, Im not new to Java, i know how to add to the List using a vector but it only works when I add to the List within the ControlIL function, however, outside of the ControlIL function(where I have all my actionPerformed code and other logic and functions, anytime I try to add to the list using Items.setListDa ta(vector), that does not work, I get null pointer excep.; and when I try model._____ nothing even pops up as a possibility for the use of model, the variable cannot be found.

    Any help would be greatly appreciated!

    Thank You!
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by wishbone34
    Hi, here is some of my code

    public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

    private JList Items;

    //builds the UI
    public ControlIL(){

    DefaultListMode l model = new DefaultListMode l();
    JList Items = new JList(model);
    }



    I want to be able to add to the List when a certain button I have is pressed, Im not new to Java, i know how to add to the List using a vector but it only works when I add to the List within the ControlIL function, however, outside of the ControlIL function(where I have all my actionPerformed code and other logic and functions, anytime I try to add to the list using Items.setListDa ta(vector), that does not work, I get null pointer excep.; and when I try model._____ nothing even pops up as a possibility for the use of model, the variable cannot be found.

    Any help would be greatly appreciated!

    Thank You!
    Well, if you use that list in the rest of your program... why don't you use that as a global....

    both of them.... the DefaultListMode l and the JList.....

    Try to have an experiment about model.addElemen t(Object) when button is pressed....

    Try to observe what happens to your list.....

    regards,
    sukatoa

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by wishbone34
      public class ControlIL extends JFrame implements ActionListener, ListSelectionLi stener {

      private JList Items;

      //builds the UI
      public ControlIL(){

      DefaultListMode l model = new DefaultListMode l();
      JList Items = new JList(model);
      }
      You assign your new JList to a local variable with the same name as your member
      variable. The local variable 'shadows' the member variable and cannot be seen
      and is not assigned to and therefore stays equal to null. Simply remove the
      local variable and assign to the member variable.

      kind regards,

      Jos

      Comment

      Working...