0

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Snooker.paint(Snooker.java:34)

this is error what I've got.

Here is the code:

Main Class:

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();
    }
}

Graphics Class:

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);          
    }
}

Also there are 5 more classes. Cue,Ball,Table and CueBall(extends Ball), BroadCloth(extends Table) . There have just attitude of their objects.

advice to solve?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
A.D
  • 7
  • 5
  • 1
    You didn't initialize `cue`. See also [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Alexis C. Mar 30 '14 at 14:42

1 Answers1

1

You have to initialize cue in the constructor of the class Snooker.

Your constructor should be:

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

As it stands, cue has not been instantiated, and throws a NullPointerException when you try to access its methods.

robertoia
  • 2,301
  • 23
  • 29