0

I am trying to create buttons dynamically in a loop from an array. I keep getting java NullPointerException error even after I read and followed the instructions on this post where someone had a similar problem and was advised to allocate the elements within his array. I did that but am still getting the same error. Can someone please show me where am going wrong? Here is my code:

@Override
protected void onMain_SavePersonAction(final Component c, ActionEvent event) {

    // declares an array of integers
    final String[] anArray;

    // allocates memory for 8 values
  anArray = new String[]{"100","200","400","500","600","700","800","900"};      

    Button[] button = new Button[anArray.length];
    for (int i = 0; i < anArray.length; i++) {
        button[i].setIcon(fetchResourceFile().getImage("personIcon.png"));
        button[i].setText("Member: "+i);
        button[i].addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            findName().setText("Firstname ");
            findVorname().setText("Secondname ");
            findFamMname().setText("Firstname Secondname" );
            findFamMname().setIcon(fetchResourceFile().getImage("personIcon.png"));
            findFamMname2().setText("Firstname Secondname ");
            findFamMname2().setIcon(fetchResourceFile().getImage("personIcon.png"));
            findDeleteMember().setVisible(true);

            c.getComponentForm().revalidate();
            c.getComponentForm().repaint();

           }
        });
        findFamilyMembers().addComponent(button[i]);

     }

    c.getComponentForm().revalidate();
    c.getComponentForm().repaint();

}
2
  • What line throws the exception? Commented Sep 29, 2014 at 0:46
  • button[i].setIcon(fetchResourceFile().getImage("personIcon.png")); when I comment it out then the line after that does the same,so its all the lines with button[i]. Commented Sep 29, 2014 at 6:51

1 Answer 1

1

You're using items in your Button[] array before creating any items and putting them in the array. Think of an array of objects similar to an empty egg carton. You can't make an omelettes using the egg carton, until you first fill it with eggs!

Change this:

for (int i = 0; i < anArray.length; i++) {
    button[i].setIcon(fetchResourceFile().getImage("personIcon.png"));

to this:

for (int i = 0; i < anArray.length; i++) {
    button[i] = new Button(); // you need to first create and assign a button object!
    button[i].setIcon(fetchResourceFile().getImage("personIcon.png"));

More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should inspect the line carefully that throws the exception, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.