【问题标题】:Dynamic Array Creation causing ArrayIndexOutOfBoundsException & NullPointerException动态数组创建导致 ArrayIndexOutOfBoundsException & NullPointerException
【发布时间】:2013-11-15 18:42:53
【问题描述】:

好的。我因为无法自己看到这一点而感到非常愚蠢,但我被困住了。第一个代码块与发生错误的其余代码相关。提前致谢! :D

此代码确定将出现多少怪物(数组的大小)。

    public static byte monsters()
{
    byte b = -7;                                //identifies how many monsters will be found

    chance = (byte)d20.nextInt(30);     //dice roll

    if(chance >= 24)
    {
        b = 4;          //quadruple monsters
    }           
    else if(chance >= 18)
    {
        b = 3;          //triple monsters
    }
    else if(chance >= 12)
    {
        b = 2;          //double monsters
    }
    else if(chance >= 6)
    {
        b = 1;          //single monster
    }
    else
    {
        b = 0;          //no monster
    }
    return b;
}//end monsters()

此代码确定并生成要放入数组的怪物。第一部分从上述代码中获取输出以确定大小。第二部分生成怪物。当“NullPointerException”被抛出时,这是它指向的代码,特别是“for(x=0;x

public void determineMons()
{
    byte x = 0;                         //counter

    switch(monsters())                  //defines the array
    {
        case 4:
            monsters = new Monster[4];
            break;

        case 3:
            monsters = new Monster[3];
            break;

        case 2:
            monsters = new Monster[2];
            break;

        case 1:
            monsters = new Monster[1];
            break;
    }//end switch

    for(x=0;x<monsters.length;x++)          //populates the array
    {
        chance = (byte)d20.nextInt(20);     //dice roll
        if(chance >= 15)
        {
            monsters[x] = new NazRuel();
        }           
        else if(chance >= 10)
        {
            monsters[x] = new GiantSnake();
        }
        else if(chance >= 5)
        {
            monsters[x] = new Yeti();
        }
        else
        {
            monsters[x] = new Zombie();
        }
    }//end fill For
}//end determineMons()

这里是“ArrayOutOfBoundsExceptions”代码。错误在不同情况之间反弹,但每次错误行都是“monster =”行。

        determineMons();
    switch(Cell.monsters())
    {
        case 4:
            monster = "There is a " + monsters[0] + ", a " + monsters[1] + ", a " + monsters[2] + ", and a " + monsters[3] + " in this area!";
            break;

        case 3:
            monster = "There is a " + monsters[0] + ", a " + monsters[1] + ", and a " + monsters[2] + " in this area!";
            break;

        case 2:
            monster = "There is a " + monsters[0] + ", and a " + monsters[1] + " in this area!";
            break;

        case 1:
            monster = "There is a " + monsters[0] + " in this area!";
            break;
    }//end Monster block

【问题讨论】:

  • 你考虑过调试吗?
  • 不要切换一个值只是为了将该值用作文字:直接使用该值
  • 啊,对不起。我什至没有在这里看到你们所有人的cmets。 Cell.monsters() 是发布的第一个代码块。我曾考虑过调试,但我不知道如何使用调试器,在课堂、工作和项目之间,我目前没有时间自学。谢谢你想帮忙。 :D

标签: java arrays nullpointerexception indexoutofboundsexception


【解决方案1】:

NullPointerException

如果monsters()返回0,则变量monsters没有被开关初始化(没有case 0也没有default),当你在for循环中执行monsters.length时会生成一个NullPointerException

另外,你应该改变你的开关:

switch(monsters())                  //defines the array
{
    case 4:
        monsters = new Monster[4];
        break;

    case 3:
        monsters = new Monster[3];
        break;

    case 2:
        monsters = new Monster[2];
        break;

    case 1:
        monsters = new Monster[1];
        break;
}

到:

int b = monsters();
if (b > 0) {
    monsters = new Monsters[b];
}

这不是一个错误,但这会让你的代码更清晰。

越界异常

您拨打monsters() 两次。换句话说,您首先调用monsters() 创建数组monsters,然后再调用monsters() 来读取monsters 的内容以确定元素的数量。

例如,假设第一次调用monsters(),返回值为3。此时,您创建了一个包含 3 个元素的数组。而当你想打印monsters的内容时,monsters()返回4。因此,在尝试读取第 4 个元素时会出现越界错误。

提醒一下,monsters() 返回的值是随机的:

chance = (byte)d20.nextInt(30);     //dice roll

为了解决这个问题,尝试改变:

switch(Cell.monsters())

switch(monsters.length)

【讨论】:

  • if (b) { ... } 不会编译。这是 Java,整数不是布尔值。
  • 啊啊啊,真的!我不敢相信我没有注意到这一点。这样的事情让我觉得我应该放弃成为一名程序员。不过谢谢。这解决了一切。 :D
猜你喜欢
  • 1970-01-01
  • 2016-12-19
  • 2013-10-13
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
相关资源
最近更新 更多