java线程“main”中的异常java.lang.NullPointerException

时间:2014-03-19 10:45:03

标签: java exception

我是Java的新手,刚刚尝试了zetcode.com的教程。他正在他的教程中解释和展示如何创建一个简单的破砖游戏,我阅读他的教程并按照他的方式对游戏进行编程。最后它没有工作。错误是:

  

线程“main”java.lang.NullPointerException中的异常     在javax.swing.ImageIcon。(ImageIcon.java:205)     在突围。球。(Ball.java:14)     at java.awt.Container.addNotify(Container.java:2769)     在javax.swing.JComponent.addNotify(JComponent.java:4741)     at java.awt.Container.addNotify(Container.java:2769)     在javax.swing.JComponent.addNotify(JComponent.java:4741)     at java.awt.Container.addNotify(Container.java:2769)     在javax.swing.JComponent.addNotify(JComponent.java:4741)     在javax.swing.JRootPane.addNotify(JRootPane.java:756)     at java.awt.Container.addNotify(Container.java:2769)     at java.awt.Window.addNotify(Window.java:770)     at java.awt.Frame.addNotify(Frame.java:487)     在java.awt.Window.show(Window.java:1031)     at java.awt.Component.show(Component.java:1651)     at java.awt.Component.setVisible(Component.java:1603)     at java.awt.Window.setVisible(Window.java:1014)     在breakout.Breakout。(Breakout.java:16)     在breakout.Breakout.main(Breakout.java:20)   线程“Timer-0”中的异常java.lang.NullPointerException     在breakout.Board $ ScheduleTask.run(Board.java:95)     在java.util.TimerThread.mainLoop(Timer.java:555)     在java.util.TimerThread.run(Timer.java:505)

因为我是新手我不知道如何解决这个问题。根据这个问题,我复制并粘贴了教程的代码,以排除代码中出错的可能性

这是使用java代码的教程:

http://zetcode.com/tutorials/javagamestutorial/breakout/

我使用Netbeans IDE 8.0

如果有人可以帮助我解决这个问题,那将是很好的:)

编辑:

因为要求我粘贴所有我的代码,直到现在这里:

Commons.java

package breakout;

public interface Commons {
public static final int WIDTH = 300;
public static final int HEIGHT = 400;
public static final int BOTTOM = 390;
public static final int PADDLE_RIGHT = 250;
public static final int BALL_RIGHT = 280;
}

Sprite.java

package breakout;
import java.awt.Image;
import java.awt.Rectangle;

public class Sprite {   //Basis Klasse für alle Objekte
protected int x;
protected int y;
protected int width;
protected int height;
protected Image image;

public void setX(int x){    //Jedes Objekt hat einen X-Wert
    this.x = x;        
}
public int getX(){
    return x;
}
public void setY(int y){    //Jedes Objekt hat einen Y-Wert
    this.y = y;        
}
public int getY(){
    return y;
}
public int getWidth(){      //Jedes Objekt hat eine Weite
    return width;
}
public int getHeight(){     //Jedes Objekt hat eine Höhe
    return height;
}
Image getImage(){           //Jedes Objekt hat ein Bild zur darstellung
    return image;
}
Rectangle getRect(){        //Jedes Objekt wird mit einem Rectangle definiert 
    return new Rectangle(x,y,
        image.getWidth(null), image.getHeight(null));
}
}

Brick.java

package breakout;
import javax.swing.ImageIcon;

public class Brick extends Sprite {
String brickie = "../../images/brickie.png";
boolean destroyed;

public Brick(int x, int y){
    this.x = x;
    this.y = y;

    ImageIcon ii = new ImageIcon(this.getClass().getResource(brickie));
    image = ii.getImage();

    width = image.getWidth(null);
    height = image.getHeight(null);

    destroyed = false;
}
public boolean isDestroyed(){
    return destroyed;
}
public void setDestroyed(boolean destroyed){
    this.destroyed = destroyed;
}
}

Ball.java

package breakout;
import javax.swing.ImageIcon;

public class Ball extends Sprite implements Commons{
private int xdir;
private int ydir;

protected String ball = "../../images/ball.png";

public Ball(){
    xdir = 1;
    ydir = -1;

    ImageIcon ii = new ImageIcon(this.getClass().getResource(ball));
    image = ii.getImage();

    width = image.getWidth(null);
    height = image.getHeight(null);

    resetState();  
}
public void move(){
    x += xdir;
    y += ydir;

    if(x == 0){
        setXDir(1);
    }
    if(x == BALL_RIGHT){
        setXDir(-1);
    }
    if(y == 0){
        setYDir(1);
    }
}
public void resetState() 
{
  x = 230;
  y = 355;
}
 public void setXDir(int x)
{
  xdir = x;
}

public void setYDir(int y)
{
  ydir = y;
}

public int getYDir()
{
  return ydir;
}
}

Paddle.java

package breakout;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Paddle extends Sprite implements Commons{
String paddle = "../../images/paddle.png";

int dx;

public Paddle(){
    ImageIcon ii= new ImageIcon(this.getClass().getResource(paddle));
    image = ii.getImage();

    width = image.getWidth(null);
    height = image.getHeight(null);

    resetState();
}
public void move(){
    x += dx;
    if(x <= 2)
        x = 2;
    if(x >= Commons.PADDLE_RIGHT)
        x = Commons.PADDLE_RIGHT;
}
public void keypressed(KeyEvent e){
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT){
        dx = -2;
    }
    if(key == KeyEvent.VK_RIGHT){
        dx = 2;
    }
}
public void keyReleased(KeyEvent e){
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT){
        dx = 0;
    }
    if(key == KeyEvent.VK_RIGHT){
        dx = 0;
    }
}
public void resetState(){
    x = 200;
    y = 360;
}
}

Board.java

package breakout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

public class Board extends JPanel implements Commons{
Image ii;
Timer timer;
String message = "Game Over";
Ball ball;
Paddle paddle;
Brick bricks[];

boolean ingame = true;
int timerId;

public Board(){
    addKeyListener(new TAdapter());
    setFocusable(true);

    bricks = new Brick[30];
    setDoubleBuffered(true);
    timer = new Timer();
    timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
}   
public void addNotify(){
    super.addNotify();
    gameInit();
}
public void gameInit(){
    ball = new Ball();
    paddle = new Paddle();

    int k = 0;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 6; j++) {
            bricks[k] = new Brick(j * 40 + 30, i * 10 + 50);
            k++;
        }
    }
}    
public void paint(Graphics g){
    super.paint(g);

    if(ingame) {
        g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
                    ball.getWidth(), ball.getHeight(), this);
        g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
                    paddle.getWidth(), paddle.getHeight(), this);
        for (int i = 0; i < 30; i++) {
            if (!bricks[i].isDestroyed())
                g.drawImage(bricks[i].getImage(), bricks[i].getX(),
                            bricks[i].getY(), bricks[i].getWidth(),
                            bricks[i].getHeight(), this);
    }
}else{
    Font font = new Font("Verdana",Font.BOLD, 18);
    FontMetrics metr = this.getFontMetrics(font);
    g.setColor(Color.BLACK);
    g.setFont(font);
    g.drawString(message,
                (Commons.WIDTH - metr.stringWidth(message))
                /2,Commons.WIDTH /2);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter{

    public void keyReleased(KeyEvent e){
        paddle.keyReleased(e);
    }        
    public void keyPressed(KeyEvent e){
        paddle.keypressed(e);
    }
}
class ScheduleTask extends TimerTask{

    public void run(){
        ball.move();
        paddle.move();
        checkCollision();
        repaint();
    }

}
public void stopGame(){
    ingame = false;
    timer.cancel();
}
public void checkCollision(){
    if (ball.getRect().getMaxY() > Commons.BOTTOM){
        stopGame();
    }
    for(int i = 0, j = 0; i < 30; i++){
        if(bricks[i].isDestroyed()){
            j++;
        }
        if(j == 30){
            message = "Victory";
            stopGame();
        }
    }
    if ((ball.getRect()).intersects(paddle.getRect())) {

        int paddleLPos = (int)paddle.getRect().getMinX();
        int ballLPos = (int)ball.getRect().getMinX();

        int first = paddleLPos + 8;
        int second = paddleLPos + 16;
        int third = paddleLPos + 24;
        int fourth = paddleLPos + 32;

        if (ballLPos < first) {
            ball.setXDir(-1);
            ball.setYDir(-1);
        }

        if (ballLPos >= first && ballLPos < second) {
            ball.setXDir(-1);
            ball.setYDir(-1 * ball.getYDir());
        }

        if (ballLPos >= second && ballLPos < third) {
            ball.setXDir(0);
            ball.setYDir(-1);
        }

        if (ballLPos >= third && ballLPos < fourth) {
            ball.setXDir(1);
            ball.setYDir(-1 * ball.getYDir());
        }

        if (ballLPos > fourth) {
            ball.setXDir(1);
            ball.setYDir(-1);
        }


    }


    for (int i = 0; i < 30; i++) {
        if ((ball.getRect()).intersects(bricks[i].getRect())) {

            int ballLeft = (int)ball.getRect().getMinX();
            int ballHeight = (int)ball.getRect().getHeight();
            int ballWidth = (int)ball.getRect().getWidth();
            int ballTop = (int)ball.getRect().getMinY();

            Point pointRight =
                new Point(ballLeft + ballWidth + 1, ballTop);
            Point pointLeft = new Point(ballLeft - 1, ballTop);
            Point pointTop = new Point(ballLeft, ballTop - 1);
            Point pointBottom =
                new Point(ballLeft, ballTop + ballHeight + 1);

            if (!bricks[i].isDestroyed()) {
                if (bricks[i].getRect().contains(pointRight)) {
                    ball.setXDir(-1);
                }

                else if (bricks[i].getRect().contains(pointLeft)) {
                    ball.setXDir(1);
                }

                if (bricks[i].getRect().contains(pointTop)) {
                    ball.setYDir(1);
                }

                else if (bricks[i].getRect().contains(pointBottom)) {
                    ball.setYDir(-1);
                }

                bricks[i].setDestroyed(true);
            }
        }
    }
}
}

Breakout.java

package breakout;

import javax.swing.JFrame;

public class Breakout extends JFrame {

public Breakout()
{
    add(new Board());
    setTitle("Breakout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(Commons.WIDTH, Commons.HEIGHT);
    setLocationRelativeTo(null);
    setIgnoreRepaint(true);
    setResizable(false);
    setVisible(true);
}

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

除了我自己编辑的图像路径外,所有代码都应该像教程一样。

3 个答案:

答案 0 :(得分:1)

我认为你没有brickie资源。 您只需要在资源"../images/brickie.png"

中的该路径上拥有图像

答案 1 :(得分:1)

只是编译器无法找到您为设置图像提供的ImageIcon源。 做一件事来检查这些东西。有其他图像URL并将其设置为该位置。并再次运行它。

答案 2 :(得分:0)

您获得的异常是告诉您ImageIcon.java(内部Java类)的第205行引用了null对象。阅读错误,下一个引用在你的代码中,第14行的Ball.java。

这是一行:

ImageIcon ii = new ImageIcon(this.getClass().getResource(ball));

这里发生的事情是this.getClass().getResource(ball)的调用正在返回null并且它会导致程序崩溃。这可能是因为您已设置为&#34; ../ images / brickie.png&#34;的相对路径。实际上并不正确。

我建议尝试使用该文件的绝对路径来实现此目的。