This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Trouble with buttonhandler - Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"" Watch "Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"" New topic
Author

Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello there, I'm really stuck here, so I hope someone wants to help me out.

I'm buidling a calculator and I'm having trouble with assignen ActionEvents to the buttons. I have a ButtonHandler class. I want to use the ButtonHandler class to hold the code for the calculating and action events. And want to assign these events to JButtons that are generated in another class called Buttons.

Furthermore I have a Display class which has to give feedback of me pressing the buttons on the calculator. All of these classes get assembled within a class called GUI.

When I run the GUI, a pretty cool Calculator GUI opens on my screen But when I press a button on the calculator, the button will not use the ButtonHandler to show the buttons label on the display, instead the compiler says I'm a fool and throws an exception:



What am I doing wrong? I have all my classes down below, I don't know what's relevant anymore, I have done a lot of testing, but the results don't get any better.

I hope you don't mind me pasting the whole classes in this topic, they aren't that big, so I think it's the best this way.

GUI


Display


Buttons


ButtonHandler


Thanks again for your precious time and effort!

Patrick


------------------8<------------------
please cut here
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

The exception you get is a NullPointerException. That means you are trying to use a reference that was never assigned to an Object. The first line of the exception's stack trace tells you the error occurs on line 20 of the Buttons class, inside the actionPerformed method. That line accesses the handler variable and tries to call a method on it. So there must be a problem with the handler variable.

When you trace the use of the handler variable you see that it never gets set. You have a method which is supposed to set the handler, but that method never gets called.

So what do you do to fix it?


Steve
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello Steve, thanks for your quick reply and sorry for my late reply. I've been rather busy with using your advise to fix my problem (and my kids, who had their Christmas holiday's )

Again I've learned a lot, I have made my code a little shorter, but I couldn't fix the problem.

I understand what you mean that the reference is NULL, I forgot to create an Object of the ButtonHandler class by saying ButtonHandler handler = new ButtonHandler();, and I need to invoke the setButtonHandler(); to connect add the functionality of the ButtonHandler class to my Buttons.

But I just don't know how and in what class to fix this. I would say I need to do this in the GUI class, because that's where everything comes together, but I tried to add code to different classes in different combinations, but everytime I get the same NPE. I hope you can help a little bit further with this.

The <---- marks what I added in my GUI class, at the moment this is the only addition to the classes above. Offcourse I had a lot more changed in the other classes, but I rolled them back. It's typical that I get the same NPE over and over again, I hope you can help me a little bit further.

GUI
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Are you sure you are getting the same NPE, and not a new NPE at a different location?
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Steve Luke wrote:Are you sure you are getting the same NPE, and not a new NPE at a different location?


Your right, it's not exactly the same error. I get an extra NPE:



I think my problem maybe in the setFeedback(); of the Display class, because when I replace the display.setFeedback(buttons.getButton(i).getText()); by a system.print.outln, it prints a String just fine by clicking a button.

I've changed my code a bit, I think it's better I put them here. I wanted to update my first post, but that's not possible anymore.

GUI


Display


Buttons


ButtonHandler
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

display and buttons are both null because you haven't set them anywhere.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Please avoid long lines; they make the code difficult to read. I have had to insert new lines and delete whitespace to maintain legibility and to retain the line no 20 where your first Exception occurred

Style matters: you are writing String buttonLabels[] in one place and JButton[] button elsewhere. Please be consistent about the placement of the []; "String[] buttonLabels" is the recommended form.
The variable buttonlabels ought to have a capital L after the n.

Several examples of poor design:
You have all sorts of coupling between classes; you are correctly using buttonLabels.length in the Buttons class, then writing 27 in the other class, without saying where the 27 comes from.

Then you have written the loop to iterate an array incorrectly. It should reador, if possibleNot <=. I don't think that would cause a NullPointerException, more likely an ArrayIndexOutOfBoundsException.
It looks very peculiar that you are setting buttons in the GUI from the ButtonHandler class. The GUI class should set up its own buttons.

You need to go through the line where the NullPointerException occurs, which you can identify from the stack trace you posted. Something to the left of a . is still null when you reach that particular line.
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
@Ritchie Campbell, thanks for the eyopeners, I wasn't aware of the inconsistencies, I will fix them as you advised. I am aware of the poor design, posts like yours really help me bring more structure in my programs. Thanks!

Rob Prime wrote:display and buttons are both null because you haven't set them anywhere.


Thanks for your reply Rob, can you be more specific? What do you mean with the display and the buttons are still null? I thought making objects of them and using the setter method in the GUI class would be enough?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Your GUI class initializes both its display and buttons fields, yes. Your ButtonHandler class doesn't initialize its buttons and display fields, so they remain null. These two fields are not the same as the fields of the GUI class with the same name.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
In the ButtonHandler class you posted, you declare the display field in line 6, but nowhere do you apply a value to it. Similarly for the buttons field. They are two fields in different classes; instantiating and assigning a field in Class1 does not affect a similarly-named field in Class2. These fields are specific to their "owning" classes, not available in other classes.

I would suggest you can dispense with the ButtonHandler class altogether; it doesn't do anything. The GUI class should add the buttons to itself.
I don't like seeing for (i = 24; i <= 26; i++) . . . That code will be very difficult to maintain and prone to errors if you add a button towards the beginning of the array. It would appear to me you ought to have two Button arrays. Then you can iterate the whole of each array, adding their buttons in the appropriate locations. And keeping the adding inside the GUI class spares you from having to pass details to another class.
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
So I see, I made a little mess. I can figure out most of your comments, but I don't know what you mean with the next line:

I would suggest you can dispense with the ButtonHandler class altogether; it doesn't do anything. The GUI class should add the buttons to itself.


It probably the word 'dispense' why I don't understand you, I can't find a proper translation for that word that makes this sentence clear for me. Should I give up the ButtonHandler + the Buttons class and merge them with the GUI class?

Your talking about the ButtonHandler class, but it looks like you mean the Buttons class to me. I hope you want to clear this out for me.

Thanks you all for your time and patience with me!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Patrick de Kruijf wrote:It probably the word 'dispense' why I don't understand you, I can't find a proper translation for that word that makes this sentence clear for me.

Toss it. Shift-delete it. Get rid of it.

Should I give up the ButtonHandler + the Buttons class and merge them with the GUI class?

In other words, yes.

Your talking about the ButtonHandler class, but it looks like you mean the Buttons class to me. I hope you want to clear this out for me.

I'd actually get rid of both of them, and merge both with the GUI class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
 
Similar Threads
communicating/affecting another class
exception in thread main problem
Not able to access an Array of buttons from another class
what layouts to use
what i have done