在switch语句中获取NullPointerException

时间:2018-07-08 22:57:35

标签: java nullpointerexception switch-statement

我最近遇到了switch语句的问题,其中编译器给了我NullPointerException:

//In the player class
public void equipArmor(Item armor)
    {
        if(armor instanceof Armor)
        {
            //Here I get the NullPointerException
            switch (armor.getArmorPartID())
            {
                case HEAD:
                    //The code...
                    break;

                case CHEST:
                    //The code...
                    break;

                case LEGS:
                    //The code
                    break;

                case ARMS:
                    //The code...
                    break;

                //Probably redunant ^^
                default:
                    break;

            }
        }

        else
        {
            System.out.println("This item is not an armor part");
        }





//Main Method
public static void main(String[] args)
{
    Player player = new Player();
    Armor cap = new Armor(//Constructor Values);
    player.equipArmor(cap);
}

//The Item Class
public class Item implements IItem
{
    boolean isWeapon;
    boolean isArmor;
    public String name;
    public int trueDamage;
    private boolean inPlayerInv = false;
    private EnumArmorPart armorPartID;

    Item(String name)
    {
        this.name = name;
    }

    @Override
    public boolean inPlayerInv()
    {
        return inPlayerInv;
    }

    @Override
    public boolean isArmor()
    {
        return isArmor;
    }

    public void setInPlayerInv(boolean inPlayerInv)
    {
        this.inPlayerInv = inPlayerInv;
    }

    @Override
    public boolean isWeapon()
    {
        return isWeapon;
    }

    @Override
    public EnumArmorPart getArmorPartID()
    {
        return this.armorPartID;
    }

    @Override
    public int getTrueDamage()
    {
        return this.trueDamage;
    }
}

Armor扩展了Item,它确实使存货变得容易(也许不是很重要...)。

//The Armor Class
public class Armor extends Item
{
    private double protection;
    private EnumAttack protectionType;
    private EnumArmorPart armorPartID;

    public Armor(double material, EnumAttack attack, EnumArmorPart armorPart, String name)
    {
        super(name);

        armorPartID = EnumArmorPart.HEAD;

        switch(armorPart)
        {
            case HEAD:
                protection = material * 2 - 2;
                break;

            case ARMS:
                protection = material * 1.5;
                break;

            case LEGS:
                protection = material * 2;
                break;

            case CHEST:
                protection = material * 4;
        }

        this.protectionType = attack;
        this.armorPartID = armorPart;
        this.isWeapon = false;
        this.isArmor = true;
    }
}

由于随后出现的问题,我尝试过但无法工作或不想做的事情:

1)我不想使Armor成为没有超类的类,因为那样它就不能与清单一起使用,因为它只存储实例Item或其子类的对象。

2)在if语句中将其设为&& armor != null仅使我“永远是对的”。

3)我试图在项中分配armorPartID = EnumArmorPart.Chest,但是Armour Cunstructor不会将其更改为我为其分配上限的ArmorPartID。

我知道,这是很多代码,但是我希望这足以使您对我的问题有所帮助:)。如果缺少什么,请编辑我的帖子,我真的很想解决我的问题!感谢所有帮助我或至少尝试<3

的人

0 个答案:

没有答案