• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code compiles but gives error when I run it  RSS feed

 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am finishing up a GUI that randomly rolls a dice and shows you the dice face from a file that I have on my computer. It compiles fine, but when I open the GUI and press the button "Roll" it gives me errors and does not display the images. Help please :\




These are the errors I get, I did go to the lines and try to fix them but when I changed the lines I either got the errors or nothing happening with the GUI.
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is label has not been initialized : NullPointer.

And the way you have this coded will leak all kinds of memory, eventually starving out the JRE of available heap.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ronald. Welcome to the Ranch!

As Roger says, you haven't initialised your label. The error message gives you a lot of information about what's happening.
That tells you what the error is (NullPointerException) and exactly where it happened (line 46). That line is:
There's only one way that line can possibly throw a NullPointerException - if label is null. So the next thing to look at is, where do you initialise that variable?
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger Sterling wrote:The problem is label has not been initialized : NullPointer.

And the way you have this coded will leak all kinds of memory, eventually starving out the JRE of available heap.


Yeah, I'm still kind of new at this. But anyways I put the private static JLabel label; and diceRoll = new JLabel();
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald chu wrote:I put the private static JLabel label;

That declares label, but doesn't assign anything to it, so it's null and can't be dereferenced.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Ronald. Welcome to the Ranch!

As Roger says, you haven't initialised your label. The error message gives you a lot of information about what's happening.
That tells you what the error is (NullPointerException) and exactly where it happened (line 46). That line is:
There's only one way that line can possibly throw a NullPointerException - if label is null. So the next thing to look at is, where do you initialise that variable?


Ok so I initialized one label in the if(roll ==1) and the rest of those if statements and one in actionperformed. Where should I initialize the label?
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
ronald chu wrote:I put the private static JLabel label;

That declares label, but doesn't assign anything to it, so it's null and can't be dereferenced.


Okay so I tried again, and this time I put


But still having no luck
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you're trying to create a new JLabel in your action listener. Shouldn't you just be changing an existing JLabel?

Which would mean that your initialization code should be creating a JLabel and putting it into the GUI. Your original code declared a variable for that JLabel but didn't do anything with it.
 
ronald chu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't understand why you're trying to create a new JLabel in your action listener. Shouldn't you just be changing an existing JLabel?

Which would mean that your initialization code should be creating a JLabel and putting it into the GUI. Your original code declared a variable for that JLabel but didn't do anything with it.


Yeah I deleted the diceRoll = new JLabel, any idea how to display the images in the if statements? :\
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!