简体   繁体   English

Java HashMap问题中的Zuul Remake

[英]Zuul Remake in Java HashMap Issue

I am doing a piece of coursework in Java using BlueJ. 我正在使用BlueJ在Java中完成一部分课程。 We need to add new features to the text based game, Zuul. 我们需要为基于文本的游戏Zuul添加新功能。 I have decided to start working on an inventory and item system. 我决定开始研究库存和物料系统。 I am having trouble working out the best way to do this so I just winged it. 我在寻找最好的方法时遇到了麻烦,所以我只管了一下。 Here is my code. 这是我的代码。 Sorry I haven't gotten round to commenting everything yet. 抱歉,我还没有发表评论。 The game compiles but I get an exception in the console when I run the game. 游戏可以编译,但是运行游戏时控制台出现异常。

Error: 错误:

java.lang.NullPointerException
    at Game.createPlayer(Game.java:15)
    at Game.<init>(Game.java:7)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:740)

Game Class (This is equivalent of the Main class in Java, this is where the game is run from): 游戏类(与Java中的Main类等效,这是运行游戏的来源):

import java.util.*;

public class Game
{
    public Game()
    {
        createPlayer();
        createItems();
    }

    private Entity localPlayer;

    public void createPlayer(){
        Player localPlayer = new Player("Player Name", 0, 0, 0, 0, 0);
        localPlayer.equipArmour("Helm", armourDB.get("Helm")); // This is where I think I have gone wrong
    }

    // Create global hashmap variables
    private HashMap<String, Weapon> weaponsDB;
    private HashMap<String, Armour> armourDB;
    private HashMap<String, Supplement> supplementDB;

    public void createItems(){
        // Create weapons
        weaponsDB = new HashMap<String, Weapon>();

        Weapon weaponFists = new Weapon("Fists", "Weapon", 0, 0, 0, 0, 0, "Melee");
        Weapon weaponSword = new Weapon("Sword", "Weapon", 0, 0, 0, 0, 0, "Melee");
        Weapon weaponBow = new Weapon("Bow", "Weapon", 0, 0, 0, 0, 0, "Ranged");
        Weapon weaponDagger = new Weapon("Dagger", "Weapon", 0, 0, 0, 0, 0, "Melee");

        weaponsDB.put("Fists", weaponFists);
        weaponsDB.put("Sword", weaponSword);
        weaponsDB.put("Bow", weaponBow);
        weaponsDB.put("Dagger", weaponDagger);

        // Create armour
        armourDB = new HashMap<String, Armour>();

        Armour armourBreastplate = new Armour("Breatplate", "Chest", 0, 0, 0, 0, 0);
        Armour armourHelm = new Armour("Helm", "Head", 0, 0, 0, 0, 0);

        armourDB.put("Breastplate", armourBreastplate);
        armourDB.put("Helm", armourHelm);

        // Create supplements
        supplementDB = new HashMap<String, Supplement>();

        Supplement supplementHealthPotion = new Supplement("Health Potion", 0, 0);
        Supplement supplementPowerPotion = new Supplement("Power Potion", 0, 0);

        supplementDB.put("Health Potion", supplementHealthPotion);
        supplementDB.put("Power Potion", supplementPowerPotion);
    }
}

Entity Class (Construction for the player class and enemy class): 实体类(玩家类和敌人类的构造):

import java.util.*;

public class Entity
{
    private boolean entityStatus;

    private String entityName;
    private int entityHealth;
    private int entityPower;
    private int entityHealthRegen;
    private int entityPowerRegen;
    private int entityAttackPower;

    private HashMap<String, Armour> entityEquipment;
    private ArrayList<Item> entityInventory;    

    public Entity(
        String paramEntityName,
        int paramEntityHealth,
        int paramEntityPower,
        int paramEntityHealthRegen,
        int paramEntityPowerRegen,
        int paramEntityAttackPower)
    {
        entityStatus = true;

        entityName = paramEntityName;
        entityHealth = paramEntityHealth;
        entityPower = paramEntityPower;
        entityHealthRegen = paramEntityHealthRegen;
        entityPowerRegen = paramEntityPowerRegen;
        entityAttackPower = paramEntityAttackPower;

        entityEquipment = new HashMap<String, Armour>(); // Set all possible equipment slots to null on initial run
        entityEquipment.put("Head", null);
        entityEquipment.put("Shoulders", null);
        entityEquipment.put("Chest", null);
        entityEquipment.put("Hands", null);
        entityEquipment.put("Legs", null);
        entityEquipment.put("Feet", null);
        entityEquipment.put("Weapon", null);

        entityInventory = new ArrayList<Item>();
    }

    public boolean getEntityStatus(){
        return entityStatus;
    }

    public String getEntityName(){
        return entityName;
    }

    public int getEntityHealth(){
        return entityHealth;
    }

    public int getEntityPower(){
        return entityPower;
    }

    public int getEntityHealthRegen(){
        return entityHealthRegen;
    }

    public int getEntityPowerRegen(){
        return entityPowerRegen;
    }

    public int getEntityAttackPower(){
        return entityAttackPower;
    }

    // Equips the player with an item into the equipment slot
    public void equipArmour(String paramEquipmentSlot, Armour paramArmourName){
        entityEquipment.put(paramEquipmentSlot, paramArmourName);
    }

    public void printInventory(){
        System.out.println("Something");
    }
}

I think the main problem is that I cannot wrap my head around the use of hashtags, I need to see a live example to see how it works. 我认为主要的问题是我无法全神贯注地使用井号,我需要看一个实时示例以了解其工作原理。 Can anyone help? 有人可以帮忙吗? If you need anything else from me, let me know. 如果您还需要我提供其他任何信息,请告诉我。

Well this is the problem: 好吧,这就是问题所在:

armourDB.get("Helm")

You haven't initialized armourDB at that point. 此时您尚未初始化armourDB If you call createItems() before createPlayer() it should be okay for that particular line. 如果在createPlayer() createItems()之前调用createItems() ,则对于该特定行应该没问题。 But you still won't be initializing the isntance variable called localPlayer . 但是您仍然不会初始化名为localPlayeristance变量。 You'll only be assigning a value to the local variable declared in createPlayer . 您将只为createPlayer声明的局部变量赋值。

It's not really clear what you're trying to achieve to be honest, but those are the first two problems... 坦白说,您目前还不清楚要达到的目标,但这是前两个问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM