游戏抛出NullPointerException

时间:2014-01-13 05:38:17

标签: java swing nullpointerexception

所以我正在做一个叫做闪避的小游戏,长方形从“天空”掉下来,你必须躲闪它们。当我运行游戏时,这个错误消息刚刚弹出,我不知道为什么。每当我运行程序时,屏幕也会变白。

我无法弄清楚这一点,我在代码中搜索了几个小时,无法弄清楚原因。请停止

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at dodge.Enemy.move(Enemy.java:22)
    at dodge.Enemy.draw(Enemy.java:32)
    at dodge.EnemyManager.draw(EnemyManager.java:34)
    at dodge.Dodge.paint(Dodge.java:38)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5226)
    at 

很抱歉,很久了。但这是我的代码。

Dodge.java

package dodge;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
public class Dodge extends JPanel  implements KeyListener{

    private Player player;
    private Stage stage;

    private EnemyManager manager;

    public Dodge(){
        setSize(new Dimension(800, 600));
        setPreferredSize(new Dimension(800, 600));
        addKeyListener(this);
        setFocusable(true);

        stage = new Stage();
        player = new Player(this, 200, 200);
        manager = new EnemyManager(this, 10);
    }

    @Override
    public void update(Graphics g){
        paint(g);
    }
    @Override
    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());

        stage.draw(g);
        player.draw(g);
       manager.draw(g);
        g.dispose();
        repaint();
    }
 @Override
   public void keyReleased(KeyEvent e){
          player.setXD(0);
          player.setYD(0);
   }

    @Override
    public void keyTyped(KeyEvent e){

   }

    public Stage getStage(){
        return stage;
    }

    @Override
    public void keyPressed(KeyEvent e){
       int c = e.getKeyCode();
       if(c == KeyEvent.VK_W){

       }
       if(c ==KeyEvent.VK_A){
       player.setXD(-1);
       }
       if(c == KeyEvent.VK_S){

       }
       if(c ==KeyEvent.VK_D){
       player.setXD(1);
       }
    }

public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Dodge the Rectangles");
        frame.add(new Dodge());
        frame.pack();
        frame.setResizable(false);
        frame.setSize(new Dimension(800, 600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

EnemyManager.java

package dodge;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class EnemyManager {

    private int amount;
    private List<Enemy> enemies = new ArrayList<Enemy>();
    private Dodge instance;

    public EnemyManager(Dodge instance, int a){
        this.amount = a;
        spawn();
        this.instance = instance;
    }

    private void spawn(){
        Random random = new Random();
        int ss = enemies.size();
        if(ss<amount){
            for(int i = 0; i < amount - ss; i++){
                enemies.add(new Enemy(instance, random.nextInt(778), 10));
            }
        }else if(ss>amount){
            for(int i = 0; i < ss - amount; i++){
                enemies.remove(i);
        }
    }
    }
    public void draw(Graphics g){
        for(Enemy e : enemies) e.draw(g);
    }
}

Enemy.java

package dodge;

import java.awt.*;

public class Enemy extends Entity{

    private Rectangle hitbox;
    private int ix, iy;
    private boolean dead = false;
    private Dodge instance;

    public Enemy(Dodge instance, int x, int y){
        super(x, y);
        this.instance = instance;
        hitbox = new Rectangle (x, y, 32, 32);

        ix = 0;
        iy = 1;
    }

    private void move(){
        if(instance.getStage().isCollided(hitbox)){
            iy = 0;
            dead = true;
        }
        hitbox.x += ix;
        hitbox.y +=iy;
    }
    private boolean isDead() {return dead;}

     @Override
    public void draw(Graphics g){
        move();
        g.setColor(Color.CYAN);
        g.fillRect(hitbox.x, hitbox.y, hitbox.width, hitbox.height);
    }

}

Entity.java

package dodge;

import java.awt.*;

public abstract class Entity {

    protected int x, y, w, h;
    protected boolean removed = false;

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

    }

    public void draw(Graphics g){

    }

    public int getX() {return x;}
    public int getY(){return y;}
    public int getW() {return w;}
    public int getH(){return h;}
}

Player.java

package dodge;

import java.awt.*;

public class Player extends Entity{

    private int xd, yd;
    private Dodge instance;
    private Rectangle hitbox;

    public Player(Dodge instance, int x , int y){
        super(x, y);
        this.instance = instance;
        w = 16; h = 16;

        hitbox = new Rectangle(x, y, w, h);
    }
    public void draw(Graphics g){
        move();
        if(!instance.getStage().isCollided(hitbox)){
            yd = 1;

        }else yd = 0;
        g.setColor(Color.ORANGE);
        g.fillOval(hitbox.x, hitbox.y, hitbox.width, hitbox.height);
    }

    private void move(){
        hitbox.x += xd;
        hitbox.y +=yd;

    }

    public void setXD(int value){
        xd = value;
    }

     public void setYD(int value){
        yd = value;

    }
}

Stage.java

package dodge;
import java.awt.*;
public class Stage {

    private Rectangle platform = new Rectangle(0, 540, 800, 100);

    public Stage(){
    }

    public boolean isCollided(Rectangle entity){
        return platform.intersects(entity);
    }

    public void draw(Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(platform.x, platform.y, platform.width, platform.height);
    }

}

我希望这不会太久。感谢

2 个答案:

答案 0 :(得分:3)

阅读stacktrace。 Enemy.instanceEnemy.instance.getStage()null

答案 1 :(得分:1)

  

每当我运行程序时,屏幕也会变白。

  1. 不要覆盖update()和paint()。这是在AWT中使用的旧代码,而不是在Swing中完成自定义绘制的方式。在Swing中,您只需覆盖JPanel的paintComponent()方法,并且不要忘记调用super.paintComponent()。阅读Custom Painting上Swing教程中的部分,了解更多信息和示例。

  2. 不要在绘画方法中调用repaint()。这将导致无限循环。这不是做动画的方法。使用Swing Timer或单独的Thread来安排动画。