File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Facing Exception in a simple swing program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Facing Exception in a simple swing program" Watch "Facing Exception in a simple swing program" New topic
Author

Facing Exception in a simple swing program

lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99

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,


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Inner classes aren't cool (in this case). Inner methods are
I cleaned your code up. I tested this and it works :


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

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


luck, db
There are no new questions, but there may be new answers.
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99

@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

Joined: Feb 24, 2011
Posts: 495

No problem!
Enjoy,
cc11rocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Facing Exception in a simple swing program