This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why am I getting a NullPointerException?

 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to save some preferences when the user closes the JFrame. I've implemented the WindowEvent to pick this up and set setDefaultCloseOperation(JFrame.DO_NOTHING). I'm calling the applications variable prefs.flush() which is giving the NullPointerException. I'm guessing that the nullpointerexception is thrown because I'm not actually handling the instance of jreport.prefs that I think I am. I would like some help finding out where I'm going wrong with handling the jreport.prefs instance. I declare JReport jreport in JReportFrame (which is instantiated by JReport). And I pass the object of JReport when calling JReportFrame:


and the JReportFrame constructor that receives this:


TIA
Adam
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Cripps wrote:I want to save some preferences when the user closes the JFrame. I've implemented the WindowEvent to pick this up and set setDefaultCloseOperation(JFrame.DO_NOTHING). I'm calling the applications variable prefs.flush() which is giving the NullPointerException. I'm guessing that the nullpointerexception is thrown because I'm not actually handling the instance of jreport.prefs that I think I am. I would like some help finding out where I'm going wrong with handling the jreport.prefs instance. I declare JReport jreport in JReportFrame (which is instantiated by JReport)...

The easiest thing to do is probably to add some debugging statements to print out the contents of the possible culprits, viz:it won't tell you why, but it may help you to narrow down the problem.

Winston
 
Adam Cripps
Ranch Hand
Posts: 41
Eclipse IDE Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston. That gives as output when run - which supports my initial guess (I'm not handling the correct version of jreport). Am I correct in thinking that if I pass the jreport instance to the constructor and that with the declaration at the beginning of the class (JReport jreport;) that the whole class should be able to see jreport?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Cripps wrote:Am I correct in thinking that if I pass the jreport instance to the constructor and that with the declaration at the beginning of the class (JReport jreport;) that the whole class should be able to see jreport?

No. You have to assign the jreport you pass to your constructor to the member variable. In your constructor you need the line
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic