简体   繁体   中英

Why do I get a Null Pointer Exception?

I have this code:

Manager manager = new Manager("Name");
MyWindowListener windowListener = new MyWindowListener(); 
manager.addWindowListener(windowListener);

Eclipse writes that I have a NullPointerException in the last line. What can be the reason for that. I do have constructors in the Manager and MyWindowListener .

If it's important MyWindowListener implements WindowListener .

You get a NullPointerException because the object reference which you'd like to access/invoke (using the period operator . ) is null . In your case it's manager which is null. But if this is verified to be wrong and the first line of the stacktrace thus doesn't give any clues, then you're probably not looking at the same version of the code which was actually running.

This piece of code cannot produce NullPointerException. So, it's thrown in one of 3 places

1) Manager constructor

2) MyWindowListener constructor

3) addWindowListener method (if you've overridden it)

Show us the code of these places for further investigation.

我的主意是,您打算将侦听器存储在Manager内的容器中,但尚未在Manager构造函数中创建该容器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM