aspose file tools*
The moose likes Beginning Java and the fly likes null pointer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null pointer?" Watch "null pointer?" New topic

null pointer?

Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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:-

----------------------------------<br />SCJP 1.4
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
I agree. Here's the link:
subject: null pointer?
Similar Threads
Problem with JDK 1.4
Problem getting image from JAR file
What is wrong?!?