gpt4 book ai didi

java - 令人惊讶的是,removeChangeListener(null) 不会抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 12:17:44 24 4
gpt4 key购买 nike

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) 时到底会发生什么?

最佳答案

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

正是这个:

JTabbedPane中调用此方法:

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

其中listenerList被声明为protected EventListenerLiSTListenerList = 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;
}
...
...

因此,删除 null 只是返回而不影响监听器

关于java - 令人惊讶的是,removeChangeListener(null) 不会抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com