jose reyes
jose reyes

Reputation: 1753

Java Error: TicTacToe game with arrayList for buttons

I have a tictactoe program, and I want to minimize the ammount of code by using an arrayList and for loops for my buttons and action listeners. There is no compile error, but when I run it, it says this.

java.lang.NullPointerException
at Board.<init>(Board.java:40)
at Board.main(Board.java:166)

That is the error that BlueJ is showing me. Anyway, here is my code. I suspect it has something to do with the instance variables, and the arrayList. The error directs me to this line:

for(int i=0; i<=buttonsList.size(); i++){

Here is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

//Created class the extends JFrame, and implements action listener
public class Board extends JFrame implements ActionListener 
{
//Instance variables 
private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, reset;
private ArrayList<JButton> buttonsList;

//'playerO' uses the letter 'O', not the number zero
Icon playerO = new ImageIcon("images/playerO.jpg");
Icon playerX = new ImageIcon("images/playerX.jpg");
Icon playerN = new ImageIcon("images/reset.jpg");

//Instance variable to determine player turn
boolean firstPlayer = true;

//Constructor
public Board()
{
    //Title of Frame
    super("Gui7 - TicTacToe || Jose Reyes");

    //Created container and set the layout
    Container c = getContentPane();
    c.setLayout(new BorderLayout());

    //Created a panel, and set the layout
    JPanel gridPanel = new JPanel();
    gridPanel.setLayout(new GridLayout(3,3));

    //Created new JButtons and added them to the array list 
    for(int i=0; i<=buttonsList.size(); i++){
        JButton jBut = new JButton(playerN);
        buttonsList.add(jBut);
    }

    //Created reset button and title
    reset = new JButton("Play Again?");

    //Added buttons to panel with for loop
    for(int i=0; i<=buttonsList.size(); i++){
        gridPanel.add(buttonsList.get(i));
    }

    //Added panel and reset button to container
    c.add(gridPanel);
    c.add(reset, BorderLayout.PAGE_END);

    //Added Action Listeners with loop
    for(int i=0; i<=buttonsList.size(); i++){
        buttonsList.get(i).addActionListener(this);
    }

    //Added action listener to reset button
    reset.addActionListener(this);

    //Set the window size and set visibility
    setSize(600,600);
    setVisible(true);
}

//ActionEvents
public void actionPerformed(ActionEvent e)
{
    //Grab source
    Object src = e.getSource();

    for (int i = 0; i<=buttonsList.size(); i++){
        if(src == buttonsList.get(i)){
            if(firstPlayer){
                buttonsList.get(i).setIcon(playerO);
                firstPlayer = false;
            } else {
                buttonsList.get(i).setIcon(playerX);
                firstPlayer = true;
            }
        }
    }


    //Reset button icons with loop
    if(src == reset){
        for (int i = 0; i < 9; i++){
            buttonsList.get(i).setIcon(playerN);
        }

        firstPlayer = true;
    }

}

//Main method
public static void main (String args[])
    {
        Board t = new Board();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Upvotes: 0

Views: 162

Answers (2)

Colm Seale
Colm Seale

Reputation: 179

You never create an ArrayList instance for buttonList, you just create an empty (null) variable reference.

Change this variable assignment to create an instance of ArrayList.

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

EDIT

I believe you will then get an OutOfMemoryException here:

//Created new JButtons and added them to the array list 
for(int i=0; i<=buttonsList.size(); i++){
    JButton jBut = new JButton(playerN);
    buttonsList.add(jBut);
}

As for each iteration, you will add a button, and the arraylist size will grow indefinitely, until you are OOM. Dunno what exactly you are trying but maybe just think through the logic here again.

Upvotes: 1

D M
D M

Reputation: 1410

for(int i=0; i<=buttonsList.size(); i++){
    JButton jBut = new JButton(playerN);
    buttonsList.add(jBut);
}

I am guessing this is the source of your OutOfMemoryError. i will never be more than buttonslist.size() because every time through the loop, you add another button and thus increase the size!

Upvotes: 1

Related Questions