惊讶于removeChangeListener(null)没有抛出NullPointerException。


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

这不会导致NullPointerException

当我致电tab.removeChangeListener(null)

时,到底会发生什么

当我调用Tab.Removechangelistener(null)?

时,到底会发生什么

正是这样:

jtabbedpane 调用此方法:

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

listenerList称为 protected EventListenerList listenerList = new EventListenerList();

删除方法是:

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;
    }
    ...
    ...

所以,删除零只是返回,而侦听器不受影响

相关内容

  • 没有找到相关文章

最新更新