Вопрос

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();
    }
}
Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top