0

我已经修复了错误,但我仍然无法启动游戏

这是一个蛇游戏,我无法启动它。当我尝试在 Eclipse 上运行它时,它一直在给我

Exception in thread "main" java.lang.NullPointerException
at Snake.createSnake(Snake.java:110)
at Snake.<init>(Snake.java:95)
at Driver.main(Driver.java:6)

我只需要有人解释并帮助我解决这个问题。我问了很多朋友,我认识的没有人可以帮助我解决它。

public class Driver {


    public static void main(String[] args) {

    Snake game = new Snake();
    }

}

下面是这个 Snake 类。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.util.*;



@SuppressWarnings("serial")
class Snake extends JFrame implements KeyListener, Runnable{

JPanel gamePanel, scorePanel;
JButton extraFood;
JTextArea textAreaScore;
int x = 500;
int y = 250;
int minSnake = 3;
int directionx = 1;
int directiony = 0;
int speed = 50;
int difference = 0;
int oldx = 0;
int oldy = 0;
int score = 0;
boolean food = false;
boolean runLeft = false;
boolean runRight = true;
boolean runUp = true;
boolean runDown = true;
boolean bonusFlag = true;
Random ran = new Random();
JButton[] bc = new JButton[200];
int[] bx = new int[300];
int[] by = new int[300];
Thread myThread;
Point[] bp = new Point[300];
Point bonusp = new Point();


//initializing values
public void Values() {
    minSnake = 3;
    directionx = 10;
    directiony = 0;
    difference = 0;
    score = 0;
    food = false;
    runLeft = false;
    runRight = true;
    runUp = true;
    runDown = true;
    bonusFlag = true;
}

//sets layout of game
public Snake() {
    getContentPane().setBackground(Color.GRAY);
    getContentPane().setLayout(null);

    gamePanel = new JPanel();
    gamePanel.setBackground(Color.LIGHT_GRAY);
    gamePanel.setBounds(6, 6, 438, 223);
    getContentPane().add(gamePanel);
    gamePanel.setLayout(new GridLayout(1, 0, 0, 0));

    scorePanel = new JPanel();
    scorePanel.setBackground(Color.GRAY);
    scorePanel.setBounds(6, 241, 438, 31);
    getContentPane().add(scorePanel);
    scorePanel.setLayout(null);

    textAreaScore = new JTextArea("Your score is:" + score);
    textAreaScore.setBackground(Color.LIGHT_GRAY);
    textAreaScore.setBounds(0, 0, 303, 31);
    scorePanel.add(textAreaScore);

    //Exit Game button
    JButton btnExitGame = new JButton("Exit Game");
    btnExitGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnExitGame.setBounds(315, 0, 117, 29);
    scorePanel.add(btnExitGame);

    btnExitGame.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
            System.exit(0);

        }

    });
    setVisible(true);
    createSnake();
    extraFood = new JButton();
    extraFood.setEnabled(false);

    addKeyListener(this);
    //This starts the game
    myThread = new Thread(this);
    myThread.start();
}

   //Creates snake
public void createSnake() {
    for (int i = 0; i < 3; i++){
        bc[i] = new JButton("b" + i);
        bc[i].setEnabled(false);
        gamePanel.add(bc[i]);
        bc[i].setBounds(bx[0], by[0], 10, 10);
        bx[i + 1] = bx[i] - 10;
        by[i + 1] = by[i];

    }
}

@SuppressWarnings("deprecation")
void reset(){
    Values();
    gamePanel.removeAll();
    myThread.stop();
    createSnake();
    textAreaScore.setText("Your score is: " + score);

     myThread = new Thread(this);
     myThread.start();

}

//As snake eats food, it grows
void snakeGrow(){
    bc[minSnake] = new JButton();
    bc[minSnake].setEnabled(false);
    gamePanel.add(bc[minSnake]);

    int a = 10 + (10 * ran.nextInt(48));
    int b = 10 + (10 * ran.nextInt(23));

    bx[minSnake] = a;
    by[minSnake] = b;
    bc[minSnake].setBounds(a, b, 10, 10);
    minSnake++;
}

//Snake moving logic
void moveForward() {
    for (int i = 0; i < minSnake; i++) {
        bp[i] = bc[i].getLocation();
    }

    bx[0] += directionx;
    by[0] += directiony;
    bc[0].setBounds(bx[0], by[0], 10, 10);

    for (int i = 1; i < minSnake; i++) {
        bc[i].setLocation(bp[i - 1]);
    }

    if (bx[0] == x) {
        bx[0] = 10;
    } else if (bx[0] == 0) {
        bx[0] = x - 10;
    } else if (by[0] == y) {
        by[0] = 10;
    } else if (by[0] == 0) {
        by[0] = y - 10;
    }

    if (bx[0] == bx[minSnake - 1] && by[0] == by[minSnake - 1]) {
        food = false;
        score += 1;
        textAreaScore.setText("Your score is: " + score);
        if (score % 50 == 0 && bonusFlag == true) {
            gamePanel.add(extraFood);
            extraFood.setBounds((10 * ran.nextInt(50)), (10 * ran.nextInt(25)), 15, 15);
            bonusp = extraFood.getLocation();
            bonusFlag = false;
        }
    }

    if (bonusFlag == false) {
        if (bonusp.x <= bx[0] && bonusp.y <= by[0] && bonusp.x + 10 >= bx[0] && bonusp.y +
    10 >= by[0]) {
            gamePanel.remove(extraFood);
            score += 100;
            textAreaScore.setText("Your score is: " + score);
            bonusFlag = true;
        }
    }

    if (food == false) {
        snakeGrow();
        food = true;
    } else {
        bc[minSnake - 1].setBounds(bx[minSnake - 1], by[minSnake - 1], 10, 10);
    }



    gamePanel.repaint();
    extracted();
}

@SuppressWarnings("deprecation")
private void extracted() {
    show();
}

public void keyPressed(KeyEvent e) {
    //Move to the left when player presses the left arrow key
    if (runLeft == true && e.getKeyCode() == 37) {
        directionx = -10; //Moves to the left by 10 pixels
        directiony = 0;
        runRight = false;     
        runUp = true;      
        runDown = true;      
    }
    //Move up when player presses the up arrow key
    if (runUp == true && e.getKeyCode() == 38) {
        directionx = 0;
        directiony = -10; //Moves up by 10 pixels
        runDown = false;     
        runRight = true;      
        runLeft = true;      
    }
   //Move to the right when the player presses the right arrow key
    if (runRight == true && e.getKeyCode() == 39) {
        directionx = +10; //Moves right by 10 pixels
        directiony = 0;
        runLeft = false;
        runUp = true;
        runDown = true;
    }
    //Move down when the player presses the down arrow key
    if (runDown == true && e.getKeyCode() == 40) {
        directionx = 0;
        directiony = +10; //Moves down by 10 pixels
        runUp = false;
        runRight = true;
        runLeft = true;
    }
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public void run() {
    for (;;) {

        //Moves the snake forward
        moveForward();
        try {
            Thread.sleep(speed);
        } catch (InterruptedException ie) {
            }
        }
    }


}   
4

4 回答 4

8

问题是您没有gamePanel在构造函数中设置全局变量,而只是它的本地版本。删除JPanel变量初始化前面的,它应该可以工作。

同样适用于您的scorePaneltextAreaScore变量。

于 2013-05-06T20:37:48.450 回答
3

问题出在这里:
您定义了全局变量:

JPanel gamePanel, scorePanel;
JTextArea textAreaScore;

但是您在以下位置定义了重复项:

public Snake() {
    JPanel gamePanel = new JPanel();
    ...
    JPanel scorePanel = new JPanel();
    ...
    JTextArea textAreaScore = new JTextArea("Your score is:" + score);
    ...
}

因此,将它们更改为:

public Snake() {
        gamePanel = new JPanel();
        ...
        scorePanel = new JPanel();
        ...
        textAreaScore = new JTextArea("Your score is:" + score);
        ...
    }

而已。

于 2013-05-06T20:55:47.127 回答
3

您将视图对象(即 gamePanel、scorePanel、extraFood、textAreaScore)声明为类 Snake 的成员变量,但随后在构造函数中声明了同名的局部变量。

成员变量:

JPanel gamePanel, scorePanel;
JButton extraFood;
JTextArea textAreaScore;

局部变量:

public Snake() {
    ...
    JPanel gamePanel = new JPanel();

    ...
    JPanel scorePanel = new JPanel();
    ...

    JTextArea textAreaScore = new JTextArea("Your score is:" + score);
    ...

更改构造函数以实例化您的成员变量,如下所示:

public Snake() {
    ...
    this.gamePanel = new JPanel();

    ...
    this.scorePanel = new JPanel();
    ...

    this.textAreaScore = new JTextArea("Your score is:" + score);
    ...
于 2013-05-06T20:46:00.967 回答
2

. gamePanel_ Snake代替

JPanel gamePanel = new JPanel();

gamePanel = new JPanel();

变量scorePaneltextAreaScore具有相同的问题。


附带问题:

  • 不要KeyListeners在 Swing 应用程序中使用。它们不是为与 Swing 应用程序一起使用而设计的。请改用键绑定
  • 同样,不要将原始线程用于 Swing 应用程序,而是使用Swing 计时器
于 2013-05-06T20:36:10.557 回答