Forums Register Login

Null Pointer - My Favorite exception

+Pie Number of slices to send: Send
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:

}
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
 

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
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1036 times.
Similar Threads
Problems reading from a .txt file
Calling the method from another method within the same class
help with global array
Binary trees problem
Global Array not working
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:37:45.