Surprised that removeChangeListener(null) doesn't throw NullPointerException

93 views Asked by At
public class Test {

    public static void main(String[] args) {
        try{
        JTabbedPane tab = new JTabbedPane();
        tab.removeChangeListener(null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

this doesn't result in a NullPointerException.

what exactly happens when I call tab.removeChangeListener(null)?

1

There are 1 answers

0
ΦXocę 웃 Пepeúpa ツ On

what exactly happens when I call tab.removeChangeListener(null)?

exactly this:

in JTabbedPane this method is invoked:

public void removeChangeListener(ChangeListener l) {
    listenerList.remove(ChangeListener.class, l);
}

where the listenerList is declared as protected EventListenerList listenerList = new EventListenerList();

and the remove method is:

public synchronized <T extends EventListener> void remove(Class<T> t, T l) {
    if (l ==null) {
        // In an ideal world, we would do an assertion here
        // to help developers know they are probably doing
        // something wrong
        return;
    }
    ...
    ...

so, removing a null is just returning leaving the listener unaffected