Java 数组 - 不断收到 NullPointerException

标签 java arrays slick2d

所以我正在尝试制作一款可以生成 20 个敌人的游戏。敌人应该被加载到敌人数组中,然后游戏意味着在 for 循环中将它们绘制出来,唯一的问题是当我尝试运行时不断收到 NullPointerException for 循环。

下面代码中的行:

enemy[i] = new Enemy(enemyX, enemyY, enemySize, vx, vy);

是抛出异常的那个。我还在下面列出了完整的源代码,这样你就可以看到我正在尝试做什么,我只是被困住了,我需要一些帮助,有没有其他方法来创建敌人并将它们放置在屏幕上?

主类:

//define package
package auctus;

//imports
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

//create class
public class Auctus extends BasicGame{
    //Window dimensions and title
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final String TITLE = "Auctus 0.1";

    //Defining the player variables, obviously only one player to create so no need for
    //automation in this area
    public float playerX, playerY, playerSize;
    //Creating the enemy array
    public Enemy enemy[];
    //declaring a global variable for the number of enemies
    public int numberOfEnemies;

    //slick2D API stuff
    public Auctus(String title) {       
            super(TITLE);       
    }


    //initialisation method
    public void init(GameContainer gc) throws SlickException {          
        //setting the number of enemies
        numberOfEnemies = 20; 

        //setting the player variables dynamically each time the game is started
        playerX = (float)Math.random() * WIDTH;
        playerY = (float)Math.random() * HEIGHT;
        playerSize = (float)Math.random() * 50;

        //creating the enemies
        for(int i = 0; i < numberOfEnemies; i++){           
            float enemyX = (float)Math.random() * WIDTH;
            float enemyY = (float)Math.random() * HEIGHT;
            float enemySize = (float)Math.random() * 50;
            float vx = (float)Math.random() * 2;
            float vy = (float)Math.random() * 2;

            //this is the line that's giving me trouble
            enemy[i] = new Enemy(enemyX, enemyY, enemySize, vx, vy);        
        }       
    }

    //from here it doesn't matter, this is error free both syntactically and logically
    public void render(GameContainer gc, Graphics g) throws SlickException    {     
            drawPlayer(g, playerX, playerY, playerSize);
            for(int i = 0; i < numberOfEnemies; i++){       
                enemy[i].drawEnemies(g);        
            }               
        }

    public void update(GameContainer gc, int delta) throws SlickException {     
        updatePlayer(gc, delta, playerX, playerY, playerSize);
        for(int i = 0;i < numberOfEnemies; i++){            
            enemy[i].updateEnemies(delta);      
        }       
    }

    public void drawPlayer(Graphics g, float playerX, float playerY, float playerSize){                 
    }

    public void updatePlayer(GameContainer gc, int delta, float playerX, float playerY, float playerSize){
    }

    public void updateEnemies(GameContainer gc, int delta, float enemyX, float enemyY, float enemySize, float vx, float vy){        
        for(int i = 0; i < numberOfEnemies; i++){           
            enemyX += vx;
            enemyY += vy;
        }       
    }

    public static void main(String[] args){     
        try{    
            AppGameContainer app = new AppGameContainer(new Auctus(TITLE));
            app.start();            
        } catch(SlickException e){          
            e.printStackTrace();            
    }
  }     
} 

敌人类别:

package auctus;

//imports
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

//class declaration
public class Enemy {
    //declaring variables
    public float enemyX, enemyY, enemySize, vx, vy;

    public Enemy(float x, float y, float size, float dx, float dy){     
        enemyX = x;
        enemyY = y;
        enemySize = size;    
        vx = dx;
        vy = dy;        
    }

    public float enemyX(){      
        return enemyY;      
    }

    public float enemyY(){
        return enemyY;      
    }

    public float enemySize(){       
        return enemySize;       
    }

    public float vx(){      
        return vx;      
    }

    public float vy(){      
        return vy;      
    }

    public void setX(float x){      
        enemyX = x;     
    }

    public void setY(float y){      
        enemyY = y;     
    }

    public void setSize(float size){    
        enemySize = size;       
    }

    public void setVX(float dx){        
        vx = dx;        
    }

    public void setVY(float dy){        
        vy = dy;        
    }

    public void drawEnemies(Graphics g){        
        g.setColor(Color.red);
        g.fillRect(enemyX, enemyY, enemySize, enemySize);       
    }

    public void updateEnemies(int delta){       
        if(enemyX < 800 || enemyX > 0){         
            enemyX += vx * delta;           
        }

        if(enemyX > 800 || enemyX < 0){         
            vx = -vx;           
        }

        if(enemyY < 600 || enemyY > 0){         
            enemyY += vy * delta;           
        }

        if(enemyY > 600 || enemyY < 0){         
            vy = - vy;          
        }       
    }   
}

敌人的等级非常简单。我不明白为什么它会使其成为 NullPointerException,但也许其他人可以发现它。

我做错了什么?

最佳答案

你永远不会实例化你的数组敌人

在某些时候,在使用 enemy 之前,您需要类似:enemy = new Enemy[numEnemies];。您可以在声明中执行此操作:

// use a constant for the number of enemies:
public static final int NUM_ENEMIES = 20; 
Enemy enemy[] = new Enemy[NUM_ENEMIES];

否则,由于 enemynull - 因为您尚未实例化它 - 当您尝试索引时,您将收到 NullPointerException它。

关于Java 数组 - 不断收到 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12573220/

相关文章:

javascript - 如何在使用固定结构时在 ngFor 中将索引值增加和更新 2

java - 使用 Java 远程连接 SafeNet HSM

java - Bean 属性 'trustStore' 不可写或具有无效的 setter 方法。 setter 的参数类型与 getter 的返回类型是否匹配?

javascript - 使用具有可选属性的对象循环遍历数组,但始终完全循环

arrays - 未定义的 Angular 日历异步 http 'map'

java - 针对多个屏幕和分辨率优化 java

java - 裁剪图像会降低质量并且边框看起来很糟糕

java - 带有 json 字符串的 Android HttpURLConnection

java - 一个 BufferedImage 上的多个渐变

java - 在 Eclipse 中,我导出的可执行 jar 没有做任何事情! (LWJGL)