java.lang.NullPointerException集中游戏

时间:2014-05-23 13:57:51

标签: java swing netbeans nullpointerexception

继我之前关于这个集中游戏的问题之后,我一直在努力尝试运行它的特定部分时遇到了java.lang.NullPointerException而我无法找出导致它的原因,它只是出现了在netbeans中指向我的主要方法。

Exception in thread "main" java.lang.NullPointerException
    at concentrate.Concentrate.main(Concentrate.java:281)

这是错误出现的代码或页面,起初我认为它可能与我正在运行的playGame方法有关,但似乎没有任何改变。即使我从内部移除了所有东西,所以我再次来堆栈溢出寻求帮助。

package concentrate;
import static concentrate.HighscoreManager.scores;
import static concentrate.Score.nameField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.Logger;
import javax.swing.*;


public final class Concentrate extends JFrame implements ActionListener {  
    //below are all the variables declared for the gui and the game program itself

    private final JFrame window = new JFrame("Concentrate");
    private  final int WINDOW_WIDTH = 330; 
    private  final int WINDOW_HEIGHT = 485; 
    private JButton exitBtn, scoresBtn, resetButton;  
    public static ArrayList<Card> gameBtn;
    public static int Point = 46;
    public static int numCards = 16;
    public static JLabel Score;  
    public static GameButtonPanel gamePnl;
    private final Panel buttonPnl = new Panel();   
    private final Panel scorePnl = new Panel(); 
    private GameButtonListener buttonListener; // Listener for the button actions
    private GameButtonPanel gui;           // The GUI this game controls
    Concentrate game;


    Random rand = new Random();
    private int cardVal;
    private GameButtonPanel theGUI;

    //update score checks and sets the score on the main board 
    public static void updateScore() {
        Score.setText("Score: " + Point);

    }





    public Concentrate(){ 
        //below is calling the create gui function and set variables for it width and close operation
        createGUI();  
        createpanels(); 

        window.setTitle("Concentrate"); 
        window.setDefaultCloseOperation(EXIT_ON_CLOSE); 
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
        window.setVisible(true);
        shuffleButtons();

        gui = theGUI;

        gamePnl = new GameButtonPanel();
        game = gamePnl.game;

        buttonListener = new GameButtonListener();

        // Adds each button to the actions listener
        for(Card c : gameBtn)
            c.addActionListener(buttonListener);
    }

    //createpanels sets diffrent panels within windows to add the buttons and score on organised from top, main and bottom
    public void createpanels()
    {
        /*gameBtn = new ArrayList<>(numCards * 2);*/
        gameBtn = new ArrayList<>(numCards);
        gamePnl = new GameButtonPanel(shuffleButtons());

        //below adds the cards to the panel based on the number of them
        for(int i = 0; i < numCards; i++)
        {
            Card cb = new Card(i);
            gameBtn.add(cb); 
        }

        buttonPnl.add(scoresBtn); 
        buttonPnl.add(exitBtn);
        buttonPnl.add(resetButton);
        gamePnl.setLayout(new GridLayout(4, 4)); 

        buttonPnl.setLayout(new GridLayout(1, 0));
        scorePnl.add(Score);
        scorePnl.setLayout(new GridLayout(1, 0));
        window.add(scorePnl, BorderLayout.NORTH);
        window.add(gamePnl, BorderLayout.CENTER);
        window.add(buttonPnl, BorderLayout.SOUTH);
    }  

    public void createGUI() {  
        buttonListener = new GameButtonListener();
        Score = new JLabel("Score: " + Point);
        exitBtn = new JButton("Exit"); 
        exitBtn.addActionListener(this);
        scoresBtn = new JButton("Leaderboard");  
        scoresBtn.addActionListener(this);
        resetButton = new JButton("Reset"); 
        resetButton.addActionListener(this);
    }   

    public static ArrayList<Card> shuffleButtons() {
        Collections.shuffle(Arrays.asList(gameBtn));

        layoutButtons();
        return gameBtn;
    }

    public static void layoutButtons() {
        for (JButton button : gameBtn) {
            gamePnl.add(button);
        }
    }
    //the code below is for the lower menu buttons
    @Override
    public void actionPerformed(ActionEvent e) 
    {    
            //this is the exit button displayed onthe bottomof the gui
            if (exitBtn == e.getSource()) 
            { 
                System.exit(0);
            } 
            //this is the button at the botto of the gui which calls on the leaderboard frame
            if (scoresBtn == e.getSource()) 
            {     
                new Leaderboard().setVisible(true);
            }    
            if (resetButton == e.getSource()) 
            {     
                shuffleButtons();
            } 
    } 

    public void reset()
    {
        // Unhide all the card buttons and set them face down
        for(Card cb : gameBtn)
        {
            cb.setFaceDown();
            cb.setVisible(true);
        }
    }

    public void playGame(){
        int PairsFound;
        int stillPlaying;
        JButton buttonPressed;
        Card button1, button2; 

        stillPlaying = JOptionPane.YES_OPTION;

        while(stillPlaying == JOptionPane.YES_OPTION)
        {
            PairsFound = 0;
            buttonPressed = null;
            // Loop until all the cards are matched.

            while(PairsFound != cardVal / 2){

                // Wait for first card to be pressed
                buttonPressed = buttonListener.waitForButton();

                if(buttonPressed instanceof Card)
                {
                    button1 = (Card)buttonPressed;
                    button1.setFaceUp();
                }
                else
                {
                    if(buttonPressed.getActionCommand().equals(gameBtn))
                        System.out.println("button was"+ gameBtn +"pressed");
                    break;
                }

                // Wait for second card to be pressed
                buttonPressed = buttonListener.waitForButton();

                if(buttonPressed instanceof Card)
                {
                    button2 = (Card)buttonPressed;
                    button2.setFaceUp();
                }
                else
                {
                    if(buttonPressed.getActionCommand().equals(exitBtn))
                        stillPlaying = JOptionPane.NO_OPTION;
                    break;
                }

                // Unflip the cards if they are not equals or if they are the
                // same card (if they refer to the same object)
                if(!button1.equals(button2) || button1 == button2){
                    ((Card)button1).setFaceDown();
                    ((Card)button2).setFaceDown();
                    Point = Point - 1;
                }
                else
                {
                    button1.setVisible(false); // Hide the pair once found.
                    button2.setVisible(false);
                    PairsFound++;
                }

                // If found all matching pairs.
                if(PairsFound == cardVal / 2)
                {

                }
            }
        }
    }

    private static final Logger LOG = Logger.getLogger(Concentrate.class.getName());

    public static void main(String[] args)
    {  
        Concentrate concentrate = new Concentrate(); 
        concentrate.game.playGame();
    }
}

对不起,如果我的代码是一个混乱仍然相当新的java,仍然学习任何帮助将不胜感激谢谢。

我不会说这是一个重复的问题,因为我没有问什么是空指针具体我问是什么导致它被抛入我的java程序但是也许我错了我不记得问到底是什么这是一个NullPointerException。

2 个答案:

答案 0 :(得分:1)

显然空指针异常来自这一行:

concentrate.game.playGame();

你知道专注不是空的,因为你刚刚成功构建它;如果它没有被成功构造,则会从集中构造函数抛出异常并且不会到达此行。这意味着game为空。

现在我们回到Concentrate构造函数并查看game初始化的位置。它似乎在以下行中初始化:

game = gamePnl.game;

您没有显示gamePnl的构造函数,但结论是GamePanel构造函数无法初始化它自己的game成员,因此该成员为null并且concent.game也最终成为空。

您可能需要重构代码以创建集中游戏或游戏面板,然后将其传递给另一个的构造函数。事实上,他们似乎都指向另一方,这是一个难以正确设置的情况。

答案 1 :(得分:0)

我不知道这是不是问题 但你有一条线:         gui = theGUI;

我在代码中找不到安装变量theGUI的地方。