This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Null Pointer - My Favorite exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Pointer - My Favorite exception" Watch "Null Pointer - My Favorite exception" New topic
Author

Null Pointer - My Favorite exception

Gino Giuliani
Greenhorn

Joined: Jan 10, 2012
Posts: 20
Yes so it's that time of year again when my code just won't do as its told. Yaaay!!!
I have created 2 different data structures to hold the same data: (1) An array (works fine)
and (2) A Binary search tree. The tree seems to work fine on its own (worked fine
when I insert entries from the array built from the same list) but when I try to insert entries directly
from a file it falters, throwing a good ol' null pointer at me! I have tried to test the insert method
within the tree itself, however no test print statements seem to call. Below I have given the
error readout and the piece of code which fails. Please Help!!!

Error:
File Opened
Soren Kierkegaard was 42 years old. Born: 05 May 1813, died: 11 Nov 1855
Existentialist@29cc3436
Exception in thread "main" java.lang.NullPointerException
at ExistentialistTreeFromFile.newExistTree(ExistentialistTreeFromFile.java:23)
at ExistentialistTreeFromFile.<init>(ExistentialistTreeFromFile.java:10)
at ExistentialistTreeTester.main(ExistentialistTreeTester.java:11)




Java:

}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Gino Giuliani wrote:Yes so it's that time of year again when my code just won't do as its told. Yaaay!!!
I have created 2 different data structures to hold the same data: (1) An array (works fine)
and (2) A Binary search tree. The tree seems to work fine on its own (worked fine
when I insert entries from the array built from the same list) but when I try to insert entries directly
from a file it falters, throwing a good ol' null pointer at me! I have tried to test the insert method
within the tree itself, however no test print statements seem to call. Below I have given the
error readout and the piece of code which fails. Please Help!!!

Error:
File Opened
Soren Kierkegaard was 42 years old. Born: 05 May 1813, died: 11 Nov 1855
Existentialist@29cc3436
Exception in thread "main" java.lang.NullPointerException
at ExistentialistTreeFromFile.newExistTree(ExistentialistTreeFromFile.java:23)
at ExistentialistTreeFromFile.<init>(ExistentialistTreeFromFile.java:10)
at ExistentialistTreeTester.main(ExistentialistTreeTester.java:11)




Java:

}



The stacktrace is pretty clear -- what is it that you don't understand? Or if you understand it, what is it are you encountering that you are disagreeing with (that is causing an issue) ?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gino Giuliani
Greenhorn

Joined: Jan 10, 2012
Posts: 20
Well as you say the stacktrace seems really clear to me (I think I understand). The error comes when insert is called from the ExistentialTree class but insert seems to work fine when called outside of this class. I have tested this several times and so I don't know why it won't call.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Agree with Henry. Because you have printed several lines, you have narrowed down the scope for there to be anything null that you don’t know about.
Gino Giuliani
Greenhorn

Joined: Jan 10, 2012
Posts: 20
As I am clearly clueless, please can you just tell me or give me some idea what is going on? I have been doing this for ages.
[EDIT]
wait before you send me the answer....

...Ok now I feel stupid but I'll learn from this horrible experience, Thank you both very much for your help(?)! (although when read your replies I almost threw the laptop - glad I didn't)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Which line did the NPE occur in? You can tell without seeing the error message because there are three print/println statements which appear to have worked successfully before the Exception. So it must have occurred after those three lines. There are not that many lines to choose from.

One object on the line where the NPE occurred is probably null. If x printed out something about Kierkegaard, it is probably not null, so there are not many things left to choose from.
Sherlock Holmes wrote:How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth?
The sign of Four chapter 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Gino Giuliani wrote:
...Ok now I feel stupid but I'll learn from this horrible experience, Thank you both very much for your help(?)! (although when read your replies I almost threw the laptop - glad I didn't)


Yeah, there is no better thing than the "a-ha" moment. You will likely never make the same mistake again.

Gino Giuliani wrote:Well as you say the stacktrace seems really clear to me (I think I understand). The error comes when insert is called from the ExistentialTree class but insert seems to work fine when called outside of this class. I have tested this several times and so I don't know why it won't call.


Another trick to notice here is that "insert" is not part of the trace. If it was something in the insert() method that was null, then there should be an extra line telling you where in the insert() method that tried to dereference null.

Henry
 
GeeCON Prague 2014
 
subject: Null Pointer - My Favorite exception