For my Java project I am creating buttons from strings stored in an array as such:
public class UserInterface implements ActionListener {
ArrayList<CardButton> currButtonList; // Current list of card buttons used
public void createAndShowGUI(String[] allCards){
//unrelated to question code
//for each String in allCards[]
for(int i=0; i<allCards.length; i++){
//Assign the button names relative to which card has is being created
String name = allCards[i];
CardButton button = new CardButton(name);
//add the button to the CardPanel
button.setFaceDown();
button.setBorderPainted(false);
int width = button.getWidth();
int height = button.getHeight();
button.setSize( new Dimension( width, height ) );
//button.setPreferredSize(new Dimension(150, 150));
CardPanel.add(button);
currButtonList.add(button);
}
}
//rest of creating the Panels, more unrelated to question code
Code complies but: Exception in thread "main" java.lang.NullPointerException at memory_game.GameFunction.(GameFunction.java:47) Which is where I try to assign listeners to each object in the array list. Am I correct in assuming that the ArrayList is not having the buttons added to it correctly? If so, why? How can I make this work?