aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException received trying to change the button label Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException received trying to change the button label" Watch "NullPointerException received trying to change the button label" New topic
Author

NullPointerException received trying to change the button label

Douglas Walters
Greenhorn

Joined: Nov 05, 2008
Posts: 2
I'm getting a java.lang.NullPointerException error during runtime when trying to change the button text when clicking the button, eastButton (line 46). I placed a System.out.println statement and the text print with no errors, so I know the coding is correct (with the exception to the setLabel command).
Any ideas?
Thanks

Doug

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

line number 23 i.e. JButton eastButton = new JButton("East"); is creating the problem. This a local declaration which hides the class field eastButton. Change the statement to

eastButton = new JButton("East");


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Douglas Walters
Greenhorn

Joined: Nov 05, 2008
Posts: 2
Thanks Ankit

This worked. I'm currently going through the Heads Up book and an example they used only displayed the buttons, but no action was attached to them. I went a step foward and added the action, but didn't realize that the JButton within the method over wrote the class button. Thanks again for your help...Doug
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

It's good to experiment with the things Douglas ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException received trying to change the button label
 
Similar Threads
How To make Button Display in JTextarea
Dynamically updating frame has me stumped.
Facing Exception in a simple swing program
not able to get the coloured oval
Can't get an ActionListener to change a JLabel text