2012-03-28 8 views
0

BlueJを使用してJavaで勉強をしています。テキストベースのゲームZuulに新しい機能を追加する必要があります。私は在庫と商品システムの作業を開始することに決めました。私はこれを行うための最善の方法を取り組むことに苦労しているので、私はちょうどそれを羽ばたきました。ここに私のコードです。申し訳ありませんが、私はまだすべてのものにコメントしていません。ゲームはコンパイルされますが、ゲームを実行するとコンソールに例外が表示されます。Java HashMapのZuulリメイクIssue

エラー:(ゲームはから実行される。これは、Javaの主なクラスに相当し、これはある)

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) 

ゲームクラス:プレイヤーのための

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を初期化していない

armourDB.get("Helm") 

+0

あなたはcreatePlayer' '前createItems''呼び出す必要があり、そうでなければ、あなたのarmourDbは 'null'なのでありますプレーヤーを初期化するとき。 – Neet

答えて

1

まあこれは問題です。 の前にcreateItems()と電話すると、その特定の回線で問題はありません。しかし、iscontactという変数をlocalPlayerという変数に初期化することはありません。あなたはローカル変数createPlayerで宣言された変数にのみ値を割り当てます。

それはあなたが正直に達成しようとしているものを実際に明確ではないのですが、これらは最初の二つの問題です...

+0

私が言ったように私はかなりJavaに新しいですし、私は半分の時間をやっているのか分かりません。私はちょうど私がする必要があると思うことをし、それが動作するかどうか見る。私の計画は、すべてのアイテムを含むハッシュマップを持つことです。プレーヤーの機器や在庫に追加することができます。アドバイスをいただきありがとうございます、私はそれを並べ替えます。 –