-2

can you tell me in details how do i know if a jcheckbox is checked or not? method isSelected didn't work with me it gives me an exception while running

{
    Sandwich = new JButton("Tall");
    contentPane.add(Tall);
    Sandwitch.setBounds(350, 110, 90,40);   //in main
    Sandwitch.addActionListener(this);
}
.....

public void actionPerformed(ActionEvent event) {
    JButton clickedButton = (JButton) event.getSource();

    String  buttonText = clickedButton.getText();
    ..........
    if(clickedButton.getText()=="Sandwitch"){
        if(Ketchup.getState()&&!Garlic.getState()){//

        itm=new Item(""+m+clickedButton.getText(),3.0);
        xyz.addItem(itm);
        textArea.append(" "+clickedButton.getText()+",");
        textArea.append(" "+itm.getPrice()+"\n");
    }
    else if(!Ketchup.isSelected()&&Garlic.isSelected()){//

        ....................
    }

it gives this very long exception while running: Here

can you please help me with this problem?

The code you Boann asked me about

Sam
  • 7,252
  • 16
  • 46
  • 65

1 Answers1

1

Here is the problem:

JCheckBox Ketchup = new JCheckBox();
Ketchup.setText("Ketchup");
Ketchup.setSize(50,25);
contentPane.add(Ketchup);
Ketchup.setBounds(175, 100, 175,25);

JCheckBox Garlic = new JCheckBox();
Garlic.setText("Garlic");
Garlic.setSize(50,25);
contentPane.add(Garlic);
Garlic.setBounds(175, 120, 175,25);

Because of the "JCheckBox" in front of the assignments, this code is declaring local variables called Ketchup and Garlic. Outside the method, those variables don't exist any more.

Meanwhile, the private fields of ClassName (ProjectInterface?) have the same names but are otherwise unrelated. They are left null.

Move the above code into the ClassName constructor, and remove the "JCheckBox" in front of the assignments. So you'll have:

private JCheckBox Ketchup;
private JCheckBox Garlic;

public ClassName() {
    Ketchup = new JCheckBox();
    Ketchup.setText("Ketchup");
    Ketchup.setSize(50,25);
    add(Ketchup);
    Ketchup.setBounds(175, 100, 175,25);

    Garlic = new JCheckBox();
    Garlic.setText("Garlic");
    Garlic.setSize(50,25);
    add(Garlic);
    Garlic.setBounds(175, 120, 175,25);
}
Boann
  • 48,794
  • 16
  • 117
  • 146
  • Also, please reming OP to change that nasty `==` while comparing his strings. – Paul Samsotha Nov 29 '13 at 16:14
  • Note: I'm not sure what OP's real class name is. In the stack traces it's "ClassName" or "ProjectInterface". In the posted code it's lower camel case "className". – Boann Nov 29 '13 at 16:19
  • I was actually referring to code above in the post, not the link :) – Paul Samsotha Nov 29 '13 at 16:20
  • Thank you so much for you help, but the same exception still appears :( might the problem be in the way i used **isSelected()** method? or it's not the right method? – user3049389 Nov 29 '13 at 16:20
  • I changed it to className here to make it clear to read – user3049389 Nov 29 '13 at 16:21
  • `isSelected` is the right method. At the top of your `actionPerformed` method, can you do a `System.out.println(Ketchup); System.out.println(Garlic);`. They must be non-null. If they are null, *any* method will throw an exception. – Boann Nov 29 '13 at 16:22
  • I did it, and it printed **null** ,what should i do here? – user3049389 Nov 29 '13 at 16:28
  • I'm out of ideas. Either they are *not* the same variables, which means you have more places in your code declaring duplicate Ketchup and Garlic variables. Or, they are the same variables, but you have some code assigning null to them. – Boann Nov 29 '13 at 16:30
  • the things i did with Ketchup are : Declaring , then initializing, then S.O.P. When the user checks it, does it initialize a boolean value automatically? – user3049389 Nov 29 '13 at 16:33
  • @user3049389 If that's really the exact three things you did, then it *must work*. *It can't not work.* Use your debugger: check the values of `Ketchup` or `this.Ketchup` at various places in the code and make sure they are never null anywhere. Same for `Garlic`. – Boann Nov 29 '13 at 16:39
  • I used debugger and it pointed at **static Scanner read = new Scanner(System.in);** – user3049389 Nov 29 '13 at 16:46