0

Is it possible before adding a new element to the Jlist to "clear" the already made selection in the Jlist ? That would solve my problem I'm having. Because when I already have made a selection in the Jlist and then add a new element, it shows up a nullpointerexception. I do not have this problem when I add a new element without having selected something in the Jlist first. Only when I select something by means of a mouseclick, then I get an exception when adding a new elment. I also work with a defaultListmodel which contains the data of the list

EDIT: I've removed the listselectionlistener from the gui frame and it worked like a charm, is there a way to have a listselectionlistener that fills up textfields when selecting a jList Object AND have buttons that add/remove from the same jList in the same gui?

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    Please add the NPE stacktrace and show how are you modifying the component (see my answer about threads). – Karol Dowbecki May 22 '20 at 13:05
  • Read the section from the Swing tutorial on [How to Use Lists](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html). The `ListDemo` does exactly what you want. It works when there is a selection. In fact you must select an item in order to delete it. – camickr May 22 '20 at 14:57
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Progman May 22 '20 at 17:06

1 Answers1

0

Your are most likely modifying GUI components from outside the Event Dispatch Thread. You can't do it as it goes against the library design:

Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe": invoking them from multiple threads risks thread interference or memory consistency errors.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111