-1

I am trying to set a checkbox to being selected if it finds the word "true" at a particular point in an array called saved_mat.

System.out.println(saved_mat[0][15]);
    if(saved_mat[0][15].equals("true")){
        e1_10e.setSelected(true);
    }

The first line prints out "true" as expected, but I get a null pointer exception on the third line. The array is of size [30][24] and the first line confirms that there is something in the [0][15] element of the array. The e1_10e is defined as a JCheckbox. I am not sure how I could be getting a null pointer exception on the third line. Any help would be appreciated.

JCheckBox e1_10e = new JCheckBox("");
    e1_10e.setBounds(32, 152, 21, 23);
    jp1.add(e1_10e);

This is the code that was generated by the GUI editor to create the JCheckbox. I used this same editor for several JTextFields and was able to reference them in my code with no problem.

ghodnett
  • 35
  • 8
  • 1
    It's because `e1_10e` is null. Where do you initialise it (there has to be something like `e1_10e = new JCheckBox()` somewhere)? – assylias Jul 16 '12 at 13:02
  • public JCheckBox e1_10e; – ghodnett Jul 16 '12 at 13:03
  • The element in the array must be there if it is being printed to the screen, so the only other option is your JCheckbox is null. Can you post how you initialize it? A `JCheckBox` is an object you need to initialize it with the `new` keyword. – Hunter McMillen Jul 16 '12 at 13:03
  • Did you call the constructor for e1_10e? – Dario Jul 16 '12 at 13:03
  • looks like e1_10e is just declared but not instanciated. show the line where you create `e1_10e`. this Object is `null`. – Simulant Jul 16 '12 at 13:03
  • JCheckBox e1_10e = new JCheckBox(""); e1_10e.setBounds(32, 152, 21, 23); jp1.add(e1_10e); – ghodnett Jul 16 '12 at 13:13
  • The checkbox appears on the GUI when I run the program which makes me think that it should be able to be referenced. I could be completely wrong about that. – ghodnett Jul 16 '12 at 13:21

3 Answers3

2

When you need to call methods on any Object you need to be sure that that Object is instantiated calling the constructor. Furthermore is a good pratice to check the value of a variable before using that.

Dario
  • 5,203
  • 1
  • 23
  • 26
  • JCheckBox e1_10e = new JCheckBox(""); e1_10e.setBounds(32, 152, 21, 23); jp1.add(e1_10e); – ghodnett Jul 16 '12 at 13:21
  • The checkbox appears in the GUI when I run it. This makes me think that it is able to be referenced. Is that not correct? – ghodnett Jul 16 '12 at 13:23
  • I put in this code to check the value of the variable before I use it and I get a null pointer exception. I was wondering how I would fix this problem. Boolean checker; checker = e1_10e.isSelected(); System.out.println("[0][15]: " +checker); – ghodnett Jul 16 '12 at 13:44
  • Is you code in the same scope of the costructor call (is the same reference the one you manage in the setSelected())? I image that the JCheckBox is an instance variable. – Dario Jul 16 '12 at 13:54
  • Sorry, I'm having trouble understanding what you are asking me. The setSelected() and the isSelected() are both in the display_1011 method within the class where e1_10e is declared at the top. The checkbox is declared as public JCheckBox e1_10e; so it should be able to be referenced later on in a different method since it was declared as public. – ghodnett Jul 16 '12 at 14:07
  • I found that my error was the line JCheckbox e1_10e = new JCheckBox(""); Since the checkbox was declared earlier, all that is needed is e1_10e = new JCheckBox(""); Thank you for trying to help me out with this. – ghodnett Jul 16 '12 at 14:17
  • One trend I've been seeing is prefacing fields with "this" (i.e. use this.e1_10e instead of just e1_10e). That way, you can avoid the bug you just had. – Dennis Meng Jul 16 '12 at 16:13
0

declaration is not creating the object, add the line:

JCheckBox e1_10e = new JCheckBox();
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • I used a GUI editor to create this program. Using that same editor, I was able to reference and manipulate many textfields in my code with no problem. The checkbox is displayed on my GUI just fine. Are there different steps necessary for JCheckbox? I'm new to Java so any help would be appreciated. – ghodnett Jul 16 '12 at 13:09
  • is your line `e1_10e.setSelected(true);` before the line `e1_10e = new JCheckBox();` ? It has to be afterwards. – Simulant Jul 16 '12 at 13:15
0

I found that my error was the line

JCheckbox e1_10e = new JCheckBox("");

Since the checkbox was declared earlier, all that is needed is

        e1_10e = new JCheckBox("");
ghodnett
  • 35
  • 8