Skip to content

Commit

Permalink
fix NPE when window closing overlaps with event (#234)
Browse files Browse the repository at this point in the history
Fixes a rare occurrence of NPE when closing the Network Tree (Configure Nodes) window.

The cause was a PropertyChangeEvent arriving after the release() call had (in theory) closed everything down. That nulled-out the timer reference. This detects that condition and ignores the event with a warning in the log.
  • Loading branch information
bobjacobsen authored Oct 6, 2023
1 parent 30f2a25 commit d4e9f01
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/org/openlcb/swing/networktree/TreePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Comparator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.JButton;
Expand Down Expand Up @@ -81,6 +82,16 @@ public void propertyChange(PropertyChangeEvent e) {
// This code will delay the updating of the display by a bit and coalesces update
// commands. This way we can ensure we don't spend too much CPU repeatedly
// updating the display.

if (timer == null) {
// A release() has removed the timer, which can
// happen if property changes overlap with the window closing.
// We ignore the event with a warning.
Logger logger = Logger.getLogger(TreePane.class.getName());
logger.warning("Data change while closing configuration window ignored");
return;
}

synchronized (timer) {
needResortTree = true;
}
Expand Down Expand Up @@ -403,7 +414,7 @@ public int compare(NodeTreeRep n1, NodeTreeRep n2) {
/**
* Cleans up all property change listeners etc in preparation when closing the window.
*/
public void release() {
public void release() {
timer.cancel();
timer = null; // get a new one next time
}
Expand Down

0 comments on commit d4e9f01

Please sign in to comment.