使用数组时Java NullPointerException

时间:2014-06-10 22:15:01

标签: java nullpointerexception

我试图用Java制作一个RPG,我试图建立一个随机遭遇系统。我需要在eclipse控制台中显示敌人的名字,我知道这些值是因为我之前打印了名字而设置的,但Java给了我一个nullpointerexception。

public class Enemies {
static Enemies Slime = null;
static Enemies Rat = null;
static Enemies Goblin = null;
//static Enemies Slime, Rat, Goblin;/*, Bandits, Sand_Worm, Antlion, Sand_Golem;*/
static Enemies[] Enemies = {Slime, Rat, Goblin};/*, Bandits, Sand_Worm, Antlion,           Sand_Golem*};*/
static Enemies[] monsters = {null,null,null,null};
int Atk;
int Def;
int Spd;
int Sp_Atk;
int Sp_Def;
int Hp;
int MaxHp;
int MinHp;
double Hptrue;
int Mp;
int MaxMp;
int MinMp;
int Level;
int MinLevel;
int MaxLevel; 
double XP_Modifier;
int MaxXp;
int MaxAtk;
int MaxDef;
int MaxSpd;
int MaxSp_Atk;
int MaxSp_Def;
int MinAtk;
int MinDef;
int MinSpd;
int MinSp_Atk;
int MinSp_Def;
String Name;

static void setstats(){
    //slime
    Slime = new Enemies();
    Slime.Name = "Slime";
    Slime.MaxHp = 10;
    Slime.MaxMp = 0;
    Slime.MinHp = 5;
    Slime.MinMp = 0;
    Slime.MaxAtk = 2;
    Slime.MaxDef = 0;
    Slime.MaxSpd = 3;
    Slime.MaxSp_Atk = 1;
    Slime.MaxSp_Def = 5;
    Slime.MinAtk = 1;
    Slime.MinDef = 0;
    Slime.MinSpd = 1;
    Slime.MinSp_Atk = 0;
    Slime.MinSp_Def = 2;
    Slime.MinLevel = 1;
    Slime.MaxLevel = 3;
    Slime.MaxXp = 10;
    Slime.XP_Modifier = 0.3;

    //rat
    Rat = new Enemies();
    Rat.Name = "Rat";
    Rat.MaxHp = 15;
    Rat.MaxMp = 0;
    Rat.MinHp = 5;
    Rat.MinMp = 0;
    Rat.MaxAtk = 5;
    Rat.MinAtk = 2;
    Rat.MaxDef = 3;
    Rat.MinDef = 1;




    //goblin
    Goblin = new Enemies();
    Goblin.Name = "Goblin";
    Goblin.MaxHp = 25;
    Goblin.MaxMp = 5;
    Goblin.MinHp = 10;
    Goblin.MinMp = 0;
} 
}

这是我的敌人类

static void Encounters(){
    encounter = (Math.random() * 100);
    System.out.println(encounter);
    if(encounter * Ph10.player.Lck < 40){
        Battle.battle();
    }

}
static void monster_creation(){
    Enemies.setstats();
    monster_amount =  (int) (Math.random() * 4);
    System.out.println(monster_amount);
    for (int i = 0; i < monster_amount; i++){
            Enemies.monsters [i]= Enemies.Enemies[ (int) (Math.random() * 2)];
            System.out.println(Enemies.monsters[i].Name);


    }

我的随机化课程的相关部分。

if( Map_Movement.tilenum < 34 ){
    battlezone = 0;
    Randomization.monster_creation();
}

我战斗阶段的相关部分。

感谢任何和所有帮助!

2 个答案:

答案 0 :(得分:1)

Java中的变量从不“引用”。

static Enemies Slime = null;
static Enemies Rat = null;
static Enemies Goblin = null;

// This evaluation of the variables (e.g. Slime) happens RIGHT NOW.
static Enemies[] Enemies = {Slime, Rat, Goblin};

// Such that it is equivalent to the following
// (Which cause an NPE on Enemies.monsters[i].Name in the loop)
static Enemies[] Enemies = {null, null, null};

在创建敌人数组之前创建敌人对象,例如

static Enemies createSlime (){
   Enemies slime = new Enemies();
   // ..
   return slime;
}

// i.e.
static Enemies Slime = createSlime();
static Enemies[] Enemies = {Slime, ..};
// or even
static Enemies[] Enemies = {createSlime(), ..};

或者只有在创建数组时才将敌人添加到数组中(例如,使用List / ArrayList会更好),例如。

static Enemies[] Enemies = new Enemies[3];

static void setstats(){
    //slime
    Enemies slime = new Enemies();
    Enemies[0] = slime;
    // ..
}

还有更多可以修复的狡辩,但这应该足以进入下一个问题 - 玩得开心!

答案 1 :(得分:1)

声明

static Enemies[] Enemies = new Enemies[3];

确定方法

static void setstats(){
//slime
Slime = new Enemies();
Slime.Name = "Slime";
Slime.MaxHp = 10;
Slime.MaxMp = 0;
Slime.MinHp = 5;
Slime.MinMp = 0;
Slime.MaxAtk = 2;
Slime.MaxDef = 0;
Slime.MaxSpd = 3;
Slime.MaxSp_Atk = 1;
Slime.MaxSp_Def = 5;
Slime.MinAtk = 1;
Slime.MinDef = 0;
Slime.MinSpd = 1;
Slime.MinSp_Atk = 0;
Slime.MinSp_Def = 2;
Slime.MinLevel = 1;
Slime.MaxLevel = 3;
Slime.MaxXp = 10;
Slime.XP_Modifier = 0.3;
Enemies[0] = Slime

//rat
Rat = new Enemies();
Rat.Name = "Rat";
Rat.MaxHp = 15;
Rat.MaxMp = 0;
Rat.MinHp = 5;
Rat.MinMp = 0;
Rat.MaxAtk = 5;
Rat.MinAtk = 2;
Rat.MaxDef = 3;
Rat.MinDef = 1;
Enemies[1] = Rat

//goblin
Goblin = new Enemies();
Goblin.Name = "Goblin";
Goblin.MaxHp = 25;
Goblin.MaxMp = 5;
Goblin.MinHp = 10;
Goblin.MinMp = 0;
Enemies[2] = Goblin
} 

编辑解释 - 您真正需要做的就是不将空值放入数组Enemies中。所以我将数组的声明更改为不包含值,因为此时它们仍然是Null。敌人是在setStats方法中创建的,所以这是你应该把它放在Array中的地方。如果您有任何其他问题,请随时询问