File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 70 is:

exit.addActionListener(closeListener);

But I don't see why this is throwing an exception
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic