views:

121

answers:

3

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.

+3  A: 

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.

BalusC
Great comment about the same version of the code!
Arcturus
+3  A: 

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.

Roman
A: 

My money is on the idea that you intend to store listeners in a container inside Manager, but that you have not created the container in the Manager constructor.

DJClayworth