• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Null pointer exception thrown in paintComponent

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic