调用paint()时出现Java NullPointerException

时间:2014-03-02 22:18:38

标签: java

我的程序在尝试调用ship的paint方法时抛出NullPointerException错误。

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

public class Ship extends Polygon implements ActionListener, KeyListener {
final static Point[] shape = new Point[4];
final static Point START_POSITION = new Point(400, 300);
private int windowX;
private int windowY;
Point pull;

public Ship(int maxX, int maxY) {
    super(shape, START_POSITION, 0);

    windowX = maxX;
    windowY = maxY;

    shipInit();
    init(shape);
    Point[] pointArray = getPoints();
    pull = new Point(0, 0);

}

public void shipInit() {
    shape[0] = new Point(0, 0);
    shape[1] = new Point(30, 10);
    shape[2] = new Point(0, 20);
    shape[3] = new Point(10, 10);   


}


public void paint(Graphics brush) {
    brush.setColor(Color.white);

    Point[] points = getPoints();

    if (position.x > windowX) {
        position.x -= windowX;
    } else if (position.x < 0) {
        position.x += windowX;
    } else if (position.y > windowY) {
        position.y -= windowY;
    } else if (position.y < 0) {
        position.x += windowY;
    }

    for (int i = 0; i < points.length; i++) {
        int x1;
        int y1;
        int x2;
        int y2;
        if (i == points.length - 1) {
            x1 = (int) points[i].x;
            y1 = (int) points[i].y;
            x2 = (int) points[0].x;
            y2 = (int) points[0].y;
        } else {
            x1 = (int) points[i].x;
            y1 = (int) points[i].y;
            x2 = (int) points[i + 1].x;
            y2 = (int) points[i + 1].y;
        }

        brush.drawLine(x1, y1, x2, y2);
    }

}


public void move() {
    position.x += 2;
    position.y += 5;
    // position.y += 1;
}


public void accelerate(double acceleration) {
    pull.x += (acceleration * Math.cos(Math.toRadians(rotation)));
    pull.y += (acceleration * Math.sin(Math.toRadians(rotation)));
}


public void keyPressed(KeyEvent e) {
    int keyPressed = e.getKeyCode();
    if (keyPressed == KeyEvent.VK_W)
        System.out.println("Hi");
    if (keyPressed == KeyEvent.VK_UP) {
        accelerate(5);
        position.x += pull.x;
        position.y += pull.y;
    }
    if (keyPressed == KeyEvent.VK_RIGHT) {
        rotation += 5;
    }
    if (keyPressed == KeyEvent.VK_LEFT) {
        rotation -= 5;
    }

}

public void keyReleased(KeyEvent e) {

}
public void keyTyped(KeyEvent e) {

}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

}

该类调用paint

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

class Asteroids extends Game {
private static int frameWidth = 800;
private static int frameHeight = 600;
Ship player;

public Asteroids() {
    super("Asteroids!", frameWidth, frameHeight);
    init();
}


public void init() {
    player = new Ship(800, 600);
    this.addKeyListener(player);
    repaint();
}


public void paint(Graphics brush) {
    brush.setColor(Color.black);
    brush.fillRect(0, 0, width,height);

    brush.setColor(Color.white);
    String hi = "hi";
    brush.drawString(hi, 299, 399);

    player.paint(brush);
}


public static void main (String[] args) {
    new Asteroids();

}

}

错误表示调用player.paint(画笔)时会抛出错误。任何人都可以找出原因吗?

1 个答案:

答案 0 :(得分:0)

我之前从未编写过此代码,但是快速浏览google的世界向我解释了paint()和init()在2个不同的线程上运行。 也就是说,你的paint()方法在init()方法之前被调用。

解决这个问题的一种快速方法是在init()中设置一个布尔值 像这样:

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

class Asteroids extends Game {
private static int frameWidth = 800;
private static int frameHeight = 600;
Ship player;

private boolean hasInitialized; // Initialization check

public Asteroids() {
    super("Asteroids!", frameWidth, frameHeight);
    init();
}


public void init() {
    player = new Ship(800, 600);
    this.addKeyListener(player);
    hasInitialized = true; // We set the boolean to true to indicate that we have initialized.
    repaint();
}


public void paint(Graphics brush) {
    if (!hasInitialized) return; // Nope, not ready yet, we'll stop here.

    brush.setColor(Color.black);
    brush.fillRect(0, 0, width,height);

    brush.setColor(Color.white);
    String hi = "hi";
    brush.drawString(hi, 299, 399);

    player.paint(brush);
}


public static void main (String[] args) {
    new Asteroids();

}

}

我不是100%肯定会修复它,考虑到你已经在构造函数中有了init()。 但值得一试!

来源:Java, applet: How to block the activation of paint() before init() finishes it's work