• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

NullPointerException,why?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the IDE and found the null variables. Thank you!
 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic