• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer - My Favorite exception

 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gino Giuliani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic