This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
the exception:

init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at Intermediate4.Notepad9.addEventHandlers(Notepad9.java:70)
at Intermediate4.Notepad9.<init>(Notepad9.java:23)
at Intermediate4.Notepad9.main(Notepad9.java:85)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

the code:


[edit]Add code tags. CR[/edit]
[ September 21, 2008: Message edited by: Campbell Ritchie ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The top line of the stack trace in the error message:

at Intermediate4.Notepad9.addEventHandlers(Notepad9.java:70)

says that the NullPointerException happened on line 70 of your source file, in the method addEventHandlers(). A NullPointerException always means that something on the left side of a dot (.) or square bracket ([) is null.

You don't tell us which is line 70,and I'm not going to spend my time counting, so you'll have to find the error yourself. But I note that the code in addEventHandlers() seems to assume that addMenu() has been called before addEventHandlers() is called -- but I don't see anyplace where addMenu() is called at all.


[Jess in Action][AskingGoodQuestions]
Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
Line 70 is:

exit.addActionListener(closeListener);

But I don't see why this is throwing an exception
Ivan Vrtacnik
Greenhorn

Joined: Sep 02, 2008
Posts: 26
OK, after I added addMenu() it all works. I didn't even know I needed it, thank you!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I said you get NullPointerExceptions when something to the left of a dot (.) is null. There's only one thing to the left of a dot on this line: the member variable "exit". Where is exit assigned to? Only in the method addMenu(). If addMenu() is never called, then "exit" remains null (the value it has when the object is created.) If exit is null, and you try to call exit.addActionListener(), then you'll get a NullPointerException. And finally, as I said before, addMenu() is actually never called.

I kind of wanted you to figure this out for yourself, but now that I've explained it, do you get it?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14279
    
  21

Because the member variable 'exit' is null at that moment. You are initialising 'exit' in the addMenu method, but that method isn't called anywhere in your code.

Tip: Use code tags when you post code, so that the forum can properly format the code.

*edit*: Ok, I was too late!
[ September 21, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException