This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I've been trying out ActionListeners and run into a problem. I want pressing a button to make a label become visible and the text on the button itself to change, but I can't seem to do the second part from within the listener - I just get a nullPointerException. Can anyone tell me what I'm doing wrong?
This a very common beginner mistake. You've got both a local variable and a member variable named "revealButton". When you initialize the variable in the constructor, it's the local one that gets set; the member stays null. But the actionPerformed method uses the member, so you get a NullPointerException. Solution: change
Ah, I see. Originally I had those variables just declared in the constructor, then when I couldn't access them from other methods I pasted the declarations up top. Pretty stupid of me really but I get so excited about building a GUI that I forget that the same rules apply to Swing objects as every other kind of object! Thanks for helping me out: much appreciated.