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();
}