Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Facing Exception in a simple swing program

 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have been going through GUI/swing related topics from Head First Java. Based on those, I have tried the following small test program



The GUI comes up fine and I am able to click on the button widget added. However, in the actionPerformed() method, the print statements are working fine.
Trying to perform anything on button is giving me a nullpointerException. Since MyButtonListener is an inner class, I should be able to access both frame and button, right?
I am little confused as to why button is null here. Any help is much appreciated.
Thanks,
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner classes aren't cool (in this case). Inner methods are
I cleaned your code up. I tested this and it works :
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh chenta wrote:


Here you create a MyButtonListener with an enclosing instance of GuiTest in which button hasn't been assigned. What you want there isOr, since the enclosing scope is already "this" just
 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@john
Thanks for trying and posting the working code.

@db
I realized my silly mistake. I should have used this instead of creating a new GuiTest object again inside func(). Thanks
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem!
Enjoy,
cc11rocks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic