• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException,why?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning the "Head First JaVa",and I tried to write my own "BeatBox" codes. The BeatBox.java was compiled sucessfully and the swing Gui was successfully loaded,as the following picture:

But when I chose some checkboxs and pressed "startbutton", the program throwed a "NonPointerException"at BeatBox.makeTrack(BeatBox.java:71),I don't know why? Can somebody help me to explain this?
 
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException occurs when you use a reference that s assigned to null. Look at line 71. Which variables might be null?
 
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome number 3 to you today.
Jack Zheng wrote:..the program throwed a "NonPointerException"at BeatBox.makeTrack(BeatBox.java:71)..


If you are using an IDE, you can add debug points and run the program in debug mode. And then you can easily find out which variables having null are trying to access instance methods or fields.
If not IDE, then you can add print statements that print the state of each of the variables on console.

As user aboves has suggested to find out, do so.
 
Jack Zheng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the IDE and found the null variables. Thank you!
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!