[英]Zuul Remake in Java HashMap Issue
我正在使用BlueJ在Java中完成一部分課程。 我們需要為基於文本的游戲Zuul添加新功能。 我決定開始研究庫存和物料系統。 我在尋找最好的方法時遇到了麻煩,所以我只管了一下。 這是我的代碼。 抱歉,我還沒有發表評論。 游戲可以編譯,但是運行游戲時控制台出現異常。
錯誤:
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)
游戲類(與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);
}
}
實體類(玩家類和敵人類的構造):
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");
}
}
我認為主要的問題是我無法全神貫注地使用井號,我需要看一個實時示例以了解其工作原理。 有人可以幫忙嗎? 如果您還需要我提供其他任何信息,請告訴我。
好吧,這就是問題所在:
armourDB.get("Helm")
此時您尚未初始化armourDB
。 如果在createPlayer()
createItems()
之前調用createItems()
,則對於該特定行應該沒問題。 但是您仍然不會初始化名為localPlayer
的istance變量。 您將只為createPlayer
聲明的局部變量賦值。
坦白說,您目前還不清楚要達到的目標,但這是前兩個問題...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.