Ошибка "AWT-EventQueue-0" в пробном коде игры

Исключение в потоке "AWT-EventQueue-0" java.lang.NullPointerException в Snooker.paint(Snooker.java:34)

это ошибка, что у меня есть.

Вот код:

Основной класс:

import java.awt.BorderLayout;    
import java.awt.Color;    
import java.awt.Graphics;    
import javax.swing.JFrame;    
import javax.swing.JPanel;

public class Snooker extends JPanel {

    public static final int WIDTH = 900;
    public static final int HEIGHT = 450;
    public static final Color c = Color.black;
    public static final int SIZE_ball = 10;

    private Table table;
    private Ball ball;
    private Cue cue;
    public Snooker() {
        table = new Table(0,0,c,WIDTH,HEIGHT);
        ball = new Ball(150,150,Color.RED,SIZE_ball);
    }

    public void paint(Graphics g) {
        super.paint(g);      
        table.drawTableOn(g);             
        ball.drawBallOn(g);            
        cue.drawCueOn(g);
    }
    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Snooker");
        frame.setLayout(new BorderLayout());
        frame.setSize(WIDTH, HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        Snooker game = new Snooker();     
        frame.add(game);
        frame.setVisible(true);
        game.requestFocusInWindow();
    }
}

Графический класс:

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

public class GraphicsItem {

    protected int x,y;
    protected Color color;
    private static final int SIZE_tableX = 900;
    private static final int SIZE_Cue = 30;
    public static final int R_ball = 5;
    public static int CoorY = 150;
    public static int CoorX = 150;


    public GraphicsItem(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public void moveBallBy(int dx, int dy) {
        x += dx;
        y += dy;
    }

    public void drawTableOn(Graphics g) {
        g.setColor(color.BLACK);
        g.fillRect(x, y, SIZE_tableX, SIZE_tableX/2);
    }

    public void drawBallOn(Graphics g) {
        g.setColor(color.BLACK);
        g.fillOval(x,y,R_ball,R_ball);
    }

    public void drawCueOn(Graphics g) {
        g.setColor(color.BLACK);
        g.drawLine(x,y,SIZE_Cue,SIZE_Cue);          
    }
}

Также есть еще 5 классов. Cue, Ball, Table и CueBall(расширяет Ball), BroadCloth(расширяет Table) . Там только отношение их объектов.

совет решить?

1 ответ

Вы должны инициализировать cue в конструкторе класса Snooker.

Ваш конструктор должен быть:

public Snooker() {
    table = new Table(0,0,c,WIDTH,HEIGHT);
    ball = new Ball(150,150,Color.RED,SIZE_ball);
    cue = new Cue( ... );
}

В настоящее время cue не был создан, и выдает исключение NullPointerException при попытке доступа к его методам.

Другие вопросы по тегам