Question

I have the following problem: There are 2 JLists in my frame:

    listModel = new DefaultListModel();
    ownlistModel = new DefaultListModel();
    fillList();


    ownBookList = new JList(ownlistModel);
    ownBookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ownBookList.setLayoutOrientation(JList.VERTICAL);
    ownBookList.setSelectedIndex(-1);



    userlist = new JList(listModel);
    userlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    userlist.setLayoutOrientation(JList.VERTICAL);
    userlist.setSelectedIndex(0);

Now, these are mutually exclusive, so if someone clicks an item in the one list, the previous selection in the other list should be cleared.

  ls2Handler = new ListSelectionListener(){
            @Override
                public void valueChanged(ListSelectionEvent e) {
              if (e.getValueIsAdjusting() == false) {


            //  System.out.println(ownBookList.getSelectedValue().toString().length());
                 Global.selectedUser =  ownBookList.getSelectedValue().toString();
                 Global.selectedIndex = ownBookList.getSelectedIndex();
             userlist.clearSelection();
                  updateFields(Global.selectedUser, 1);

              }


        }
    };

        lsHandler = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                  if (e.getValueIsAdjusting() == false) {

                //  System.out.println(userlist.getSelectedValue().toString().length());

                      Global.selectedUser =  userlist.getSelectedValue().toString();
                     Global.selectedIndex = userlist.getSelectedIndex();
                   ownBookList.clearSelection();
                      updateFields(Global.selectedUser, 0);
                  }


            }
            };


        userlist.addListSelectionListener(lsHandler);
        ownBookList.addListSelectionListener(ls2Handler);

On calling the Value is adjusting function I always get the nullpointerException:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Books$3.valueChanged(Books.java:199) at javax.swing.JList.fireSelectionValueChanged(Unknown Source)

Why is that? As you can see, they have been declared and should be accessible.

Was it helpful?

Solution 2

Thanks to Robin I figured it out. I wasn't aware that the method clearselection() triggers the listener. It's quick and dirty but it works:

    ls2Handler = new ListSelectionListener(){
        @Override
            public void valueChanged(ListSelectionEvent e) {
          if (e.getValueIsAdjusting() == false) {

              if (ownBookList.getSelectedValue()!=null) {
                  userlist.clearSelection();
                  Global.selectedUser =  ownBookList.getSelectedValue().toString();
                  Global.selectedIndex = ownBookList.getSelectedIndex();
                  updateFields(Global.selectedUser);
              }

                else {

                    }

          }

    }
};

    lsHandler = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
              if (e.getValueIsAdjusting() == false) {

                  if (userlist.getSelectedValue()!=null) {
                     ownBookList.clearSelection();
                      Global.selectedUser =  userlist.getSelectedValue().toString();
                      Global.selectedIndex = userlist.getSelectedIndex();
                      updateFields(Global.selectedUser);


                  }
                  else {

                  }
              }


        }
        };

Thanks again!

OTHER TIPS

The getSelectedValue() method can return null, and you are calling toString() on that null object

userlist.getSelectedValue().toString();

See the documentation of that method:

Returns the value for the smallest selected cell index; the selected value when only a single item is selected in the list. When multiple items are selected, it is simply the value for the smallest selected index. Returns null if there is no selection.

And since you clear the selection on list B when you select something on list A, you will quickly run into this exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top