零点异常,不明白为什么?

时间:2012-11-15 16:36:18

标签: java exception-handling nullpointerexception

好的,我是java的半新手,我正在制作乒乓球游戏。我想完全靠自己做,但我遇到了问题。到目前为止,我有2节课。我的主要球员之一,其中包含有关球的信息。 我的主要课程如下:

    import java.awt.Canvas;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;


public class Main extends Canvas implements Runnable{

    private static final long serialVersionUID = 1L;

    public static int Width=800;
    public static int Height=600;
    public boolean Running=false;
    public Thread thread;
    public Ball ball;
    public int BallX = ball.BallLocationX;
    public int BallY = ball.BallLocationY;



    public static void main(String[] args){
        Main game = new Main();
        JFrame frame = new JFrame();
        frame.setSize(Width, Height);
        frame.setTitle("Pong By Poo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.add(game);
        game.start();
        }


    public void start(){
        if(Running==true){
            return;
        }
        else {
            Running=true;
            thread = new Thread(this);
            thread.start();
        }
    }



    public void run(){
        while(Running==true){
            Draw();
        }

    }

    public void Draw(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs==null){
            createBufferStrategy(2);
        }else{
            Graphics g = bs.getDrawGraphics();
            g.setColor(Color.BLACK);
            g.fillOval(BallX, BallY, 10, 10);   
        }


    }

}

我的球类是这样的:

    **import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Ball extends JPanel{
    public int BallLocationX;
    public int BallLocationY;
    public boolean BallMovementY; //true makes the ball go up, false makes it go down
    public boolean BallMovementX; //True makes the ball go right, false makes it go left.

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillOval(BallLocationX, BallLocationY, 10, 10);
    }

    //moves the ball left to right
    public int YVelocity(){

        if(BallMovementY==true){
            BallLocationY++;
        }
        else{
            BallLocationY--;
        }

        return BallLocationY;

    }

    //Moves the ball up and down
    public int XVelocity(){

        if(BallMovementX==true){
            BallLocationX++;
        }
        else{
            BallLocationX--;
        }

        return BallLocationX;

    }
}
**

我试图在我的主课堂内的屏幕上画球,使用我从球类获得的球的位置。 我知道(截至目前)球不会移动,我想出来以后。我的问题是,它不会在屏幕上画球,给我这个错误:

Exception in thread "main" java.lang.NullPointerException
    at Main.<init>(Main.java:20)
    at Main.main(Main.java:26)

谢谢!

3 个答案:

答案 0 :(得分:14)

public Ball ball;  // ball is not initialized
public int BallX = ball.BallLocationX;  // NPE here
public int BallY = ball.BallLocationY;

这是问题所在。在您的实例变量声明中,您的ball仍然指向null,并且您已使用它来访问BallLocationX。它将抛出NPE

您应首先将ball引用初始化为指向Ball的实例: -

public Ball ball = new Ball();  // Or whatever way you use to instantiate it
public int BallX = ball.BallLocationX;
public int BallY = ball.BallLocationY;

建议: -

  • 我刚注意到,您已经使用了public modifier fields。你不应该这样做。尽可能尝试 您的字段private modifier,并提供public accessors 访问他们。 ( getters and setters )。
  • 另外,请遵循Java命名约定。变量名称应以小写字母/下划线/美元符号开头(将BallX更改为ballX,将BallLocationX更改为ballLocationX

答案 1 :(得分:1)

您定义ball,但不要使用任何值对其进行初始化,然后在ball上调用方法null

public Ball ball;
public int BallX = ball.BallLocationX;

建议:请不要使用public字段,除非您有充分的理由这样做。

答案 2 :(得分:0)

frame.setLocationRelativeTo(空);可能是问题。对此进行评论并尝试。