1

わかりました。私は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)

ありがとう!

4

3 に答える 3

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

これが問題です。インスタンス変数の宣言では、 yourballはまだ を指して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にアクセスできるようにしてください。(ゲッターとセッター)。
  • また、Java 命名規則に従ってください。変数名は小文字のアルファベット/アンダースコア/ドル記号で始まる必要があります (BallXballXBallLocationX変更ballLocationX)
于 2012-11-15T16:38:16.443 に答える
1

を定義しますballが、値で初期化してから、でメソッドを呼び出さないでください。ballこれはnullです。

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

アドバイス:public非常に正当な理由がない限り、フィールドは使用しないでください。

于 2012-11-15T16:40:17.180 に答える
0

frame.setLocationRelativeTo(null); 問題になる可能性があります。これをコメントして試してみてください。

于 2012-11-15T16:38:28.627 に答える