0

I have an array full of a few buttons and I am trying to go through the array and give each button a color, however I keep getting a null point exception error.

   public static Button[] arrayButtons = new Button[33];                
   @FXML public Button btn1 = new Button();
   @FXML public Button btn2 = new Button();
   @FXML public Button btn3 = new Button();

  @FXML
  public void initializeButtonArray() {        
    arrayButtons[1] = btn1;
    arrayButtons[2] = btn2;
    arrayButtons[3] = btn3;
    arrayButtons[4] = btn4;       
  }

   @FXML
   private void test() {

      initializeButtonArray();
      for(Button btn : arrayButtons) {
          btn.setStyle("-fx-background-color: #FF0000");  //error occurs here
      }
  } 

The error occurs when I try to give all the buttons a color inside my for each loop.

2
  • 3
    You're initializing your Button array to hold 33 objects, but only assigning 4 of them. So the other 29 remain null. Also worth noting is that Java is a zero-indexed language, so the first element in your array is arrayButtons[0], not arrayButtons[1]. And finally, you're doing arrayButtons[4] = btn4, but you never created btn4 in the code you've provided, so this code will never compile as-is. Commented Mar 27, 2019 at 20:39
  • Thank you so much for your help, it works now :) Commented Mar 27, 2019 at 21:23

2 Answers 2

2

Arrays Start at 0 so you never set arrayButtons[0] to anything leaving it as null aka waiting to throw a NullPointer when called. You also never initialize btn4 which will throw a null pointer when called and lastly why are you creating a 33 button array when you only initialize 3 the others will throw do you want to guess its a NPE

try this

public static Button[] arrayButtons = new Button[4];

public void initializeButtonArray() {  
    arrayButtons[0] = btn1;      
    arrayButtons[1] = btn2;
    arrayButtons[2] = btn3;
    arrayButtons[3] = btn4;      
}

Add this

@FXML public Button btn4;

On your first iteration through the arrayButtons loop it throws a null pointer because its looking for arrayButtons[0] but its set as null which breaks your whole loop

Also if your referencing a FXML you do not put = new Button();

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

2 Comments

omg thank you so much, I'm such a noob at coding ;(, literally spent all day trying to fix this.
I'd recommend doing arrayButtons = new Button[] { btn1, btn2, btn3, btn4 }; in initializeButtonArray since this way you do not need to change 2 parts of the code when changing the number of elements of the array which decreases possible sources of errors. Also it's shorter...
1

You initialized only few elements in array. Other are referencing to null.

Change this line of code

public static Button[] arrayButtons = new Button[33];   

to

public static Button[] arrayButtons = new Button[4];   

Note that arrays starts at index 0!

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.