This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

NullPointerException

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic