0

I am making a clone of minesweeper with (slightly modified) JButtons. Because there are so many game tiles in minesweeper, I am storing them as an array. When I try and add the buttons to the Frame using a for loop, I get a nullpointerexception on the buttons. The class ButtonObject is extended from the JButton class with just two extra variables and getter/setter methods. What is going wrong?

Code:

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


public class Minesweeper extends JFrame implements ActionListener{

    JLabel starttitle;
    ButtonObject[] minefield;
    JFrame frame;
    Random r = new Random();
    int rand;
    JPanel startscreen;
    JPanel gamescreen;
    int gamesize;
    JButton ten;
    JButton tfive;
    JButton fifty;

    GridLayout layout; 



    public Minesweeper()
    {
        frame = new JFrame("Minesweeper");
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);;
        startscreen = new JPanel();
        startScreen();
    }

    public void startScreen()
    {
        ten = new JButton("10 x 10");
        tfive = new JButton("25 x 25");
        fifty = new JButton("50 x 50");
        starttitle = new JLabel("Welcome to minesweeper. Click a game size to begin.");
        frame.add(startscreen);
        startscreen.add(starttitle);
        startscreen.add(ten);
        startscreen.add(tfive);
        startscreen.add(fifty);
        ten.addActionListener(this);
        tfive.addActionListener(this);
        fifty.addActionListener(this);
    }
    public void initializeGame()
    {
        minefield = new ButtonObject[gamesize];
        for(int i = 0;i<gamesize;i++)
        {
            minefield[i]=new ButtonObject();
            rand = r.nextInt(5);
            if(rand==5)
            {
                minefield[i].setButtonType(true);//this tile is a mine
            }
        }
    }
    public void gameScreen()
    {
        frame.getContentPane().removeAll();
        frame.repaint();
        initializeGame();
        for(int i = 0;i<minefield.length;i++)
        {
            gamescreen.add(this.minefield[i]);//EXCEPTION HERE
        }

    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==ten)
        {
            gamesize = 99;
            gameScreen();
        }
        else if(e.getSource()==tfive)
        {
            gamesize = 624;

            gameScreen();
        }
        else if(e.getSource()==fifty)
        {
            gamesize = 2499;

            gameScreen();
        }
        else
        {
            System.out.println("Fatal error");
        }

    }
    public static void main(String[] args)
    {
        new Minesweeper();
    }
}

1 Answer 1

5

Well, you never initialize your gamescreen variable, so that's totally normal you get a NullPointerException at this line.

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

4 Comments

true. oops. sorry for this question, that was rather stupid of me
@imulsion there are dumb questions, but this was certainly not one :) also if this is the correct answer you should accept it
@imulsion It happens. I occasionally find myself adding elements to uninitialized collections.
@RonE I think it's that fact that one does not initialise the variables right at the start, that does throw me sometimes >.<

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.