File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception thrown in paintComponent

 
Michael Nana
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Nana
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic