Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Where have I gone wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where have I gone wrong?" Watch "Where have I gone wrong?" New topic

Where have I gone wrong?

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

I am working on an assignment in which clicking on a Team button scores a point for that team, which is displayed below the buttons. There is also a text field at the bottom that tells if one team is winning over the other.

I am convinced this code is 100% correct to reach the desired results. However, it is acting as if the buttons are not registered with any event handlers. That is, nothing at all happens when you click on anything. I could swear I registered them correctly. Furthermore, there are lines of code after it is determined which button that is pushed that when not commented out, causes an error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

I am not asking for hand outs or for somebody to do this for me. It's for extra credit anyway, so there really isn't any pressure, but I am very annoyed I'm having this problem.

Thanks in advance,


I just need some direction as to how to fix this. FWIW this was compiled with jGrasp.

Here is the code:

I will be very grateful for the help, I can't stand not being able to finish simple assignments like I thought this one was.
[ May 21, 2006: Message edited by: Brandt Charles ]
[ May 21, 2006: Message edited by: Brandt Charles ]

Green, green, green!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
duplicate declarations problem

instance variable
private JButton team1;

now in the TeamDemo() constructor
JButton team1 = new JButton("Team 1");

by including the type (JButton), you make a local variable out of team1.
team1 is created, added to the gui, and is the button you see on the screen,
but in ButtonHandler(), it is the instance variable that the souce() is
compared to, which would be null.

to fix, change
JButton team1 = new JButton("Team 1");
team1 = new JButton("Team 1");

repeat for other buttons etc
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193


OK, listen close. You've made an error that everyone makes, precisely once. It's silly, and it's embarrassing, but if you understand the problem well, you'll have really learned something, so listen up!

You've defined member variables team1, team2, and many others in your class. All the event handlers and things reference these variables. Note that when you declare them, they're all null, which is just fine -- you intend to initialize them later. No problems so far.

But in the constructor, you don't initialize them. You instead declare new local variables with the same names and you initialize those. The members remain null, and those local variables disappear as sooon as the constructor completes. The Buttons and things that those variables pointed to still exist, but no variables refer to them anymore.

This is declaring a variable and assigning it a value:

JButton team1 = new JButton("Team 1");

While this is assigning a value to a pre-existing variable, which is what you actually want to do:

team1 = new JButton("Team 1");

Putting the type (JButton) in there makes it into a declaration.

Do you understand? If you don't, speak up. If you do, congratulations -- you'll never make this silly mistake again!

[Jess in Action][AskingGoodQuestions]
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Thank you both so much for the explanation! I thought I had gone insane. This would explain why another of my GUI projects generated a similar problem as well. I had scoured over the text thinking I had not registered the controls properly, had no idea it was in my data declarations.

And Ernest, I did learn something, hopefully I won't make the mistake again.

Thanks again, Ernest and Michael.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19742

And just for future reference, here's another mistake you were making:
"I am convinced this code is 100% correct to reach the desired results."
That's a bad attitude to have when your code isn't working. Obviously your code isn't 100% correct, because it isn't working, but you're convinced it is 100% correct? One of the two must be wrong. And I can tell you, from my personal experience, the one that is wrong is almost always me. You're going to find it's the same with you.
I agree. Here's the link:
subject: Where have I gone wrong?
It's not a secret anymore!