Hello good day.

I am having trouble letting my listeners wait on each other.

I have a

  1. JComboBox
  2. JList - groups
  3. JList - students

The choice from the JComboBox, clears and updates the groups JList by way of an actionlistener. The ListSelectionListener for the groups JList, then responds to you selecting data (that was just added). The choice is refelcted in the students JList.

The trouble is I am getting a NullPointerException at random times. I believe this happens because in order to update the groups-JList via the ComboBox's ActionListener, I need to clear it's contents first. But this seems to trigger its ListSelectionListener. The trouble is that this trigger happens when it is cleared - so there's nothing in the JList and so when the ListSelectionListener runs, it is working with an empty list thus the NullPointerException.....whew.(I hope that was clear)

Please is there some way to let the Listeners wait on each other; one going only after the other is completed?

Thank you.

Your analysis of the problem seems reasonable. Why not just check the selection in JList ListSelectionListener and ignore the event if the selection is null?

@JamesCherrill please see OPs last post, seems to forgot to tell us that there is JDBC (can't found any words to wrote something cleaver there), then everything is ....??? or maybe isn't ???

Thanks for the link - hopefully OP will identify the exact source of the NPE to determine whether this is a Swing problem or something related to the underlying code.
I have had similar problems in the past - ie replace the contents of a list box -> fires the list box listener multiple times, first time with no contents. Seemed to me at the time that testing for, and ignoring, the irrelevant calls was the easiest solution.

@JamesCherrill

  • fires the list box listener multiple times ---> hehehe (I'm see that, know about) but why bothering with, is required to use MutableComboBoxModel, not to remove/add XxxListener added to JComboBox at runtime,

  • and using this model properly, with proper notifiers fireXxxXxx, then you can to manage all events that came from or stop that, including the selection in JComboBoxes view :-)

Yes, I agree with all that. He certainly should not be removing/adding listeners dynamically to fix this.

Quite a hearty discussion going on here. The second thread that @mKorbel mentioned is from the same program. But they are completely different problems.

@JamesCherrill I have realized that my analysis is right. I found a simple solution. I created a boolean variable that only turns changes until after the first action listener completes execution. And I put as a condition for the other listener that boolean variable to be a particular value. So even when the second listener fires, it doesn't run it's instructions, because all conditions aren't met.

So I've solved this problem, which is separate from that other thread I am having trouble with.

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.