• Post Reply Bookmark Topic Watch Topic
  • New Topic

"NullPointerException" - but I cant find the Error.  RSS feed

 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

I am currently writing a Vocabulary learn program for myself, and beeing done with all the Random choosing and so on the biggest part of the code is done.
You can see the code here:



(Sorry if it's messed up a bit)
The code gets started through a main in another class, the first thing runnin' is the "run()".

Now however, I keep getting a NullPointerException, together with my "Couldn't find the Vocabulary File!" and "Couldn't find the Random File!".

Konnte die Vokabel Datei nicht lesen.
java.lang.NullPointerException
at Vocabularymain.zeilegeben(Vocabularymain.java:54)
at Vocabularymain.dateiladen(Vocabularymain.java:43)
at Vocabularymain.gui(Vocabularymain.java:27)
at Vocabularymain.run(Vocabularymain.java:21)
at java.lang.Thread.run(Thread.java:722)
Konnte die Random Datei nicht lesen.
java.lang.NullPointerException
at Vocabularymain.zeilegeben2(Vocabularymain.java:75)
at Vocabularymain.randladen(Vocabularymain.java:65)
at Vocabularymain.dateiladen(Vocabularymain.java:50)
at Vocabularymain.gui(Vocabularymain.java:27)
at Vocabularymain.run(Vocabularymain.java:21)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-1" java.lang.NullPointerException
at Vocabularymain.getRandom(Vocabularymain.java:79)
at Vocabularymain.gui(Vocabularymain.java:28)
at Vocabularymain.run(Vocabularymain.java:21)
at java.lang.Thread.run(Thread.java:722)

I've checked twice, the data are there, and using a small testing program I copied out of Head first it worked:



In that case I get the both inputs I've made into the file MeinText.txt

I'd be very grateful if someone was able to help me, because I'm at the end with my knowledge right now. Eclipse didnt show me any error. The Compiler didnt show me any error (well, thats logical to be honest.)
Please note that the code is not 100% finished, so there may be some weird things.

Oh, and can you see potential where I could have saved some writing?

Thanks in advance for your help,

Joe Degler

(Please note that this is my first serious... "program", so dont be too harsh if you spot something )
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM wrote:at Vocabularymain.zeilegeben(Vocabularymain.java:54)


At line 54 of your code, then. In the "zeilegeben" method, which has only three lines of code. So load that class into your text editor and skip down to line 54. Which line is that? Because it's the line where you are trying to use a reference which is null.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So with other words the highlighted line is the problem. But I still dont get it, String linie gets the line out of the data through zeilegeben(zeile); , which starts zeilegeben(String linie). So String Linie should get the text of "zeile", lets just say "Test$This", which then gets seperated into a String array of the type String, String[0] representing Test and String[1] representing This. Or am I wrong about this?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although it's not the cause of the NullPointerException, your call to linie.split("$") is wrong. The split method takes a regular expression, and $ has a special meaning in regular expressions. You must escape it:
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Although it's not the cause of the NullPointerException, your call to linie.split("$") is wrong. The split method takes a regular expression, and $ has a special meaning in regular expressions. You must escape it:


Thank you, didn't know that, instantly corrected it!
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your line counting might be off by one. However the easiest thing to do is to put some debugging statements into your code to see what's happening. Like

for example.

Also, "String" is one of the worst possible variable names you could ever choose in Java. Whenever I see that I think of the type "String" and I have to force myself to realize that it's actually a variable name. Very confusing.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I think your line counting might be off by one. However the easiest thing to do is to put some debugging statements into your code to see what's happening. Like

for example.

Also, "String" is one of the worst possible variable names you could ever choose in Java. Whenever I see that I think of the type "String" and I have to force myself to realize that it's actually a variable name. Very confusing.


I'll try that tomorrow...
And sorry for calling it string, I just wanted to be 100% sure it's working like its in the book...
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . "String" is one of the worst possible variable names you could ever choose in Java. Whenever I see that I think of the type "String" and I have to force myself to realize that it's actually a variable name. Very confusing.
It will confuse the compiler, too; it will assume String means the variable, and may give errors if you use the type String.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here is the updated code, still with the same problem:



Now, the problem seems to be this line here:



...and the same for every other line that uses that command.

I still cant see what I'm doing wrong
Wort1[0] which has the first String out of the text data (before the first $) sends his word into the deutsche ArrayList which has Strings. Or is something wrong?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've never instantiated deutsche, spanish or alle.
 
Joe Degler
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, changing



into



solved the problem.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!