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 ]
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.
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:
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.
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.