Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer?

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. i cant get this applet running. here is the code:-

this applet have 3 JTextField which represents red, green and blue. when a user input 3 values ranging between 0 to 255 and press enter, the applet will draw a fill rectangle(100,100,100,100) on a canvas (with white background). the color of the rectangle corresponds to the value input from the user. the problem is, when enter is pressed the following exception is throwed:-
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Syamsul,
The problem is that your Graphics reference is null. In your init() method, if you will change this:

to this:

then it should work. The problem here is that if you call canvas.getGraphics() before adding it to the applet's content pane, then it's graphics has not been set.
Hope this helps,
Michael Morris
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic