wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Null pointer exception thrown in paintComponent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null pointer exception thrown in paintComponent" Watch "Null pointer exception thrown in paintComponent" New topic
Author

Null pointer exception thrown in paintComponent

Michael Nana
Greenhorn

Joined: Feb 10, 2012
Posts: 17
Hey guys,
I am working on a l-system assignment to draw strings on a panel and when you click the draw button it expands the string and redraws the diagram larger and larger.
Well I have finished coding up but I have a null pointer exception in my paintcomponent which i don't really see the reason why. I initialized all the variables I am using in the paintcomponent so I don't see why I get flagged with it. I'll greatly appreciate it if you would take a look.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Michael Nana wrote:Hey guys,
... I initialized all the variables I am using in the paintcomponent so I don't see why I get flagged with it.


That is incorrect - if you did than you would not get a NullPointerException. The key is to recognize which Object was not initialized before it is used. If you give us the error message and the line where the error occurs it will probably help solve the problem. A quick look indicates that the String instructions may not be initialized before use. It looks like it (or any of the other Strings) might not get assigned until the actionPerformed on jTextField3 gets executed... Which means that you would get this NPE anytime the DrawPanel is visible without the text getting edited before hand.


Steve
Michael Nana
Greenhorn

Joined: Feb 10, 2012
Posts: 17
Steve Luke wrote:
Michael Nana wrote:Hey guys,
... I initialized all the variables I am using in the paintcomponent so I don't see why I get flagged with it.


That is incorrect - if you did than you would not get a NullPointerException. The key is to recognize which Object was not initialized before it is used. If you give us the error message and the line where the error occurs it will probably help solve the problem. A quick look indicates that the String instructions may not be initialized before use. It looks like it (or any of the other Strings) might not get assigned until the actionPerformed on jTextField3 gets executed... Which means that you would get this NPE anytime the DrawPanel is visible without the text getting edited before hand.


Oh yeah sorry, the error is on line 265, instructions.length

Yeah I assumed that and that's why I have the jtextfield3 () method called in the paint component commented. When I remove the comment, I no longer get the NPE however the program doesn't behave like I wish. I have a method expandcode that I want it to expand the instructions when called but it doesn't do it.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You will have to do something to initialize those values prior to displaying the panel. Calling the jtextfield's action performed method at the start of the paintComponent method is probably not the correct thing to do because it could cause side effects (like re-setting the object values or a long loop where the paint triggers an action which triggers a paint ... making the application unresponsive).

The simplest thing is probably to define some meaningful default values when the Object gets first created. Like the empty String ("") or something descriptive ("Enter Text") depending on what seems to make sense in your scenario. But you would need to make meaningful defaults (meaningful == will cause expected, nice looking display and not cause errors) for all your Strings and Objects used in the paintComponent method, and these values should be assigned when the class is created / initialized and before it gets displayed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Null pointer exception thrown in paintComponent