调用paint()时出现Java NullPointerException

调用paint()时出现Java NullPointerException,java,Java,我的程序在尝试调用船舶的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 Po

我的程序在尝试调用船舶的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

}

}

该错误表示调用player.paint(画笔)时抛出该错误。有人能找出原因吗?

我以前从未编写过这样的代码,但快速浏览一下谷歌世界,我就知道paint()和init()在两个不同的线程上运行。 也就是说,在init()方法之前调用paint()方法

解决这个问题的一个快速方法是在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();

}

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


资料来源:

我不确定这是怎么可能的,但我不知道
游戏从何而来。您的
init
方法是从您的构造函数调用的,从技术上讲,这意味着它不能放在要绘制的容器上,除非您自己从
Game
中绘制它,如果
Game
扩展自任何类型的
Swing
AWT
组件,你也打破了油漆链…根本没有足够的信息来帮助你回答问题,或者知道这个答案是否正确…同意,我们需要更多的信息来帮助你解决这个问题。但我真的看不出有任何其他方式会发生这种情况。在对程序的其他部分进行了一些添加后,它已停止抛出该错误。作为记录,游戏扩展了画布。
Game
从何扩展而来?如何调用
paint
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();

}

}