Java数组-不断获取NullPointerException

Java数组-不断获取NullPointerException,java,arrays,slick2d,Java,Arrays,Slick2d,所以我想做一个游戏,让我产生20个敌人。敌人应该被加载到一个敌人数组中,然后游戏要把他们拉到一个for循环中,唯一的问题是当我尝试运行for循环时,我一直得到一个NullPointerException 代码中的行如下所示: enemy[i] = new Enemy(enemyX, enemyY, enemySize, vx, vy); 就是那个抛出异常的。我在下面列出了完整的源代码,这样你就可以看到我正在尝试做什么,我只是被卡住了,我需要一些帮助,有没有其他方法来创建敌人并将他们放在屏幕上

所以我想做一个游戏,让我产生20个敌人。敌人应该被加载到一个敌人数组中,然后游戏要把他们拉到一个for循环中,唯一的问题是当我尝试运行for循环时,我一直得到一个
NullPointerException

代码中的行如下所示:

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();            
    }
  }     
} 
//定义包
包装拍卖;
//进口
导入org.newdawn.slick.AppGameContainer;
导入org.newdawn.slick.BasicGame;
导入org.newdawn.slick.Color;
导入org.newdawn.slick.GameContainer;
导入org.newdawn.slick.Graphics;
导入org.newdawn.slick.SlickException;
//创建类
公共类Auctus扩展了BasicGame{
//窗口尺寸和标题
公共静态最终整数宽度=800;
公共静态最终内部高度=600;
公共静态最终字符串TITLE=“Auctus 0.1”;
//定义玩家变量,显然只需要创建一个玩家,因此不需要
//这方面的自动化
公共浮动播放器X、playerY、playerSize;
//创建敌人阵列
公敌[];
//声明敌人数量的全局变量
公敌;
//光滑的2D API材料
公开拍卖(字符串标题){
超级(标题);
}
//初始化方法
public void init(GameContainer gc)引发异常{
//设置敌人的数量
数量=20;
//每次游戏开始时动态设置玩家变量
playerX=(float)Math.random()*宽度;
playerY=(float)Math.random()*高度;
playerSize=(float)Math.random()*50;
//制造敌人
对于(inti=0;i
敌人阶级:

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;          
        }       
    }   
}
packageauctus;
//进口
导入org.newdawn.slick.Color;
导入org.newdawn.slick.Graphics;
//类声明
公敌{
//声明变量
公开浮动enemyX、enemyY、Enemize、vx、vy;
公敌(浮标x,浮标y,浮标大小,浮标dx,浮标dy){
enemyX=x;
灌肠y=y;
敌人化=规模;
vx=dx;
vy=dy;
}
公共浮点enemyX(){
回肠术;
}
公开募股{
回肠术;
}
公共浮点enemySize(){
还击敌人;
}
公共浮点vx(){
返回vx;
}
公共浮点数({
返回vy;
}
公共无效集合x(浮动x){
enemyX=x;
}
公共无效setY(浮动y){
灌肠y=y;
}
公共无效设置大小(浮动大小){
敌人化=规模;
}
公共void setVX(float dx){
vx=dx;
}
公共无效设置(float dy){
vy=dy;
}
公众人士(图g){
g、 setColor(Color.red);
g、 fillRect(灌肠、灌肠、灌肠、灌肠);
}
public void updateEnemies(int delta){
如果(enemyX<800 | | enemyX>0){
enemyX+=vx*δ;
}
如果(enemyX>800 | | enemyX<0){
vx=-vx;
}
如果(enemyY<600 | | enemyY>0){
Enemy+=vy*delta;
}
如果(enemyY>600 | | enemyY<0){
vy=-vy;
}       
}   
}
敌人的阶级相当直截了当。我不明白为什么它会变成一个
NullPointerException
,但也许其他人可以发现它


我做错了什么?

您从未实例化过您的阵列
敌人

在某个时候,在你使用敌方之前,你需要像这样的东西:
enem
// use a constant for the number of enemies:
public static final int NUM_ENEMIES = 20; 
Enemy enemy[] = new Enemy[NUM_ENEMIES];