0

I'm having a hard time to put action listeners on multiple check boxes, that are on a panel and bordered. Like for example : if a checkbox is selected,than it must update an int, in my case that is total, and if more than one is selected the total must be updated. This is what I'm doing :

public void actionPerformed(ActionEvent a) {
    if (hat.isSelected()) {
        total += 50;
    }
}

But I'm getting an error, One more question, how do I make it so that I can put multiple item listeners in a class. P.s : I'm a beginner at coding GUI-s in Java. Help would be very appreciated.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Projekt.actionPerformed(Projekt.java:61)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

Edit : Added Error Codes

KobraCode
  • 75
  • 2
  • 11
  • *But I'm getting an error*, Exactly which error do you get? – Thomas Fritsch Jan 29 '18 at 21:49
  • at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) there are some more lines, that I didn't copy here – KobraCode Jan 29 '18 at 21:52
  • The two line of the exception are probably the most important, they describe the error and the location. Add, at least, the first two lines – MadProgrammer Jan 29 '18 at 21:55
  • @MadProgrammer oh, sorry Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Projekt.actionPerformed(Projekt.java:61) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) – KobraCode Jan 29 '18 at 21:56
  • Please add this info as formatted text *within* your question, not as comment. – Thomas Fritsch Jan 29 '18 at 21:56
  • 1
    Okay, guess time. `hat` is probably `null` because it's never initialised. This could be because you forget or because you are shadowing your variables. Without a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) it will be impossible to provide you with any more guidance – MadProgrammer Jan 29 '18 at 22:00
  • The exception stack trace tells you: The NullPointerException happened in line 61 of your file `Projekt.java`. – Thomas Fritsch Jan 29 '18 at 22:02
  • @MadProgrammer hat is JCheckBox, if it is selected total += 50 – KobraCode Jan 29 '18 at 22:08
  • @KobraCode Irrelevant - if you've not initialised or you've shadowed an instance field, then it will be `null`, hence your error. The diagnostics of `NullPointerException` are almost always the same. You need to work you way backwards through your code and find out why the variable is not been assigned a value – MadProgrammer Jan 29 '18 at 22:20
  • @MadProgrammer I managed to fix the error code, but now when the checkbox is selected it does not update the total, the checkbox and the total is a field variable, and the total is initialized to 0 . I do really appreciate your help, thanks a lot ! – KobraCode Jan 29 '18 at 22:35

0 Answers0