0

I am trying to give each JButton from a 10 x 10 button layout (so 100 buttons) each unique name or ID or number so I can call them later. I made an ArrayList because that's what some other person did.

public ArrayList<JButton> myList;

//Some other code

        for(int row = 0; row < 10; row++)
        {
            for(int col = 0; col < 10; col++)
            {
                    button = new JButton();
                    button.addActionListener( al );
                    myList.add(button);
                    for(JButton button : myList)
                        button.setText("");
                    panel_1.add(button);
            }
        }

The program compiles but it doesn't run. It's showing error at

myList.add(button);

It's a null pointer exception apparently.

but I don't know why. Is it not adding the buttons to the ArrayList? Also how do I give each button a unique name or string?

  • `It's showing error at` - we are not (always) mind readers. Tell us the error when you ask a question. Don't expect us to guess. – camickr Apr 02 '17 at 20:28
  • @camickr it's showing a null pointer exception. I assume it's because no ArrayList has been made. – i_want_to_die Apr 02 '17 at 20:35
  • (1-) `I assume it's because no ArrayList has been made.` - so if you know what the problem is why did you ask a question??? Fix the problem!!! – camickr Apr 02 '17 at 20:49
  • @camickr I wasn't sure damn, I don't know if it's been created or not. Anyways I only found out that after I found that it was a null pointer exception. – i_want_to_die Apr 02 '17 at 21:33
  • `JButton@#setActionCommand`? – MadProgrammer Apr 02 '17 at 22:06
  • `I only found out that after I found that it was a null pointer exception` - that is my point. You stated in the question you had a NPE. So don't assume what the problem is and ask a question. Instead try solving the problem based one your assumption. In other words to some basic problem solving first before posting a question. In any case you have already been given an answer. – camickr Apr 02 '17 at 23:38

1 Answers1

1

The program compiles but it doesn't run. It's showing error at

The ArrayList is null because you didn't create an ArrayList object.

The code should be:

private ArrayList<JButton> myList = new ArrayList<JButton>();

Also how do I give each button a unique name or string?

There is no need to give the button a unique name. Its unique name is the "index" used to access the JButton in the ArrayList.

You probably don't even need the ArrayList.

Normally you add an ActionListener to the button. Then you can just use the getSource() method of the ActionEvent to get the reference to the button.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • However, you can give each button a name if you want to, using button.setName(). For example, the name could be "Row=3 Column=5". Note that this is different from the text displayed in the button, set by button.setText(). – FredK Apr 02 '17 at 20:55
  • @FredK, this has nothing to do with my suggestion. Please remove the comment. If you think this is the solution then add your own answer or add a comment to the question. – camickr Apr 02 '17 at 23:40