繁体   English   中英

调用paint()的Java NullPointerException

[英]Java NullPointerException in call to paint()

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

}

}

和班级叫油漆

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(brush)时抛出该错误。 谁能找出原因?

我以前从未编写过代码,但是快速浏览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();

}

}

考虑到构造函数中已经有init(),我不是100%肯定会解决此问题。 但这值得一试!

来源: Java,applet:如何在init()完成工作之前阻止paint()的激活

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM