2010-07-17 66 views
1

我試過的printStackTrace,我已經coverted一切靜態的(我認爲)......然而,線17和38行都是問題......因爲這個錯誤的:的Java NullPointerException異常

You picked up: Pickaxe 
java.lang.NullPointerException 
     at item.addInv(item.java:38) 
     at item.main(item.java:17) 
Description: Can be used to mine with. 
Press any key to continue . . . 

17號線: anItem.addInv(1);

第38行:arr.add("Dan");

這裏是我的代碼:

import java.io.*; 
import java.util.*; 
import javax.swing.*; 

public class item 
{ 
    public static int attack, defense; 
    public static ArrayList<String> arr; 
    public static String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,earnedCoins,canEquip; 

    String stats[]; 

    public static void main(String args[]) 
    { 
     item anItem = new item(); 
     ArrayList<String> arr = new ArrayList<String>(); 
     anItem.addInv(1); 
    } 

    public static void addInv(int e) { 
     String iname = getItem(1)[0]; 
     String idesc = getItem(1)[1]; 
     int itypeOf = Integer.parseInt(getItem(1)[2]); 
     int iattackAdd = Integer.parseInt(getItem(1)[3]); 
     int idefenseAdd = Integer.parseInt(getItem(1)[4]); 
     boolean icanSell = Boolean.parseBoolean(getItem(1)[5]); 
     boolean icanEat = Boolean.parseBoolean(getItem(1)[6]); 
     int iearnedCoins = Integer.parseInt(getItem(1)[7]); 

     attack = attack + iattackAdd; 
     defense = defense + idefenseAdd; 
     System.out.println("You picked up: " + iname); 
     try { 
      arr.add("Dan"); 
     } catch(NullPointerException ex) { 
      ex.printStackTrace(); 
     } 

     System.out.println("Description: " + idesc); 
    } 

    public static String[] getItem(int e) { 

     String[] stats = new String[7]; 

     String name = "Null"; 
     String desc = "None"; 
     String typeOf = "0"; 
     String attackAdd = "0"; 
     String defenseAdd = "0"; 
     String canSell = "true"; 
     String canEat = "false"; 
     String earnedCoins = "0"; 

     if (e == 1) { 
      name = "Pickaxe"; 
      desc = "Can be used to mine with."; 
      typeOf = "2"; 
      attackAdd = "2"; 
      earnedCoins = "5"; 
     } 

     return new String[] { name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat, earnedCoins}; 
    } 
} 

正如你所看到的那樣,這些線條,我不知道該怎麼辦...:\

+0

你的代碼是一團糟。爲什麼要在代表一個項目的對象上調用'getItem(idx)'?你已經混淆了物品的概念和庫存。 – Eric 2010-07-17 22:05:11

+1

同意 - 但OP可能是一個新的Java程序員+它會更有幫助我們提供指導而非批評。 – 2010-07-17 22:24:52

回答

1

String canEat = "false";你爲什麼要轉換字符串和從字符串轉換?

你似乎糊塗了一個item類和inventory類。

也許一個枚舉會更好:

public enum InventoryItem 
{ 
    PICKAXE("Pickaxe", "Can be used to mine with", ItemType.Tool, 
      5, 2, 0) 

    EPIC_PICKAXE("Super mega awesome Pickaxe", "Can be used to mine with, but epically", ItemType.Tool, 
      1000000, 100, 0) 


    public static enum ItemType { 
     TOOL, 
     WEAPON 
    } 

    public final String name, description; 
    public final ItemType type; 
    public final boolean canSell, canEat, canEquip; 
    public final int earnedCoins, attackAdd, defenseAdd; 

    private InventoryItem(String name, String description, ItemType type 
          int earnedCoins, int attackAdd, int defenseAdd, 
          boolean canSell, boolean canEat, boolean canEquip) 
    { 
     this.name  = name; 
     this.description = description; 
     this.type  = type 
     this.canSell  = canSell; 
     this.canEat  = canEat; 
     this.canEquip = canEquip; 
     this.earnedCoins = earnedCoins; 
    } 

    private InventoryItem(String name, String description, ItemType type 
          int earnedCoins, int attackAdd, int defenseAdd) 
    { 
     this(name, description, type, 
      earnedCoins, attackAdd, defenseAdd, 
      true, false, true); 
    } 
} 

然後,你可以有你的播放器的類中List<InventoryItem> inventory = new ArrayList<InventoryItem>(),並與直接連接。

+0

因爲返回方法只返回一種類型。 – nn2 2010-07-17 21:49:12

+0

因此返回一個對象! – Eric 2010-07-17 21:57:45

+0

「普通」類可能比枚舉更好 - 枚舉的設計是一個永久固定的選項集。 – 2010-07-17 22:26:23

2

變量arr未初始化。

變量ARR在main()不在函數addInv()

只要初始化它在addInv修復它相同編曲。

+0

「或者在main()中刪除重新聲明使它只是arr = new ArrayList();」 - 由於arr屬性超出了主方法的範圍,因此不起作用。最好的辦法是在構造函數中初始化'arr' – chrisbunney 2010-07-17 22:12:56

+0

是的,你是對的。我刪除它 – Hrishi 2010-07-17 22:27:56

2

當你在arr上調用add()方法時,它還沒有被初始化,因此NullPointerException。

既然你可能會在其他方法中使用ArrayList,你應該在構造函數中初始化它;即:

public item() { 
    arr = new ArrayList<String>(); 
} 
1

的一些技巧(一說並不直接解決的問題):

1)儘可能變量聲明爲私有,或至多保護。我個人從不使用包級別訪問的「默認」(同一包中的任何東西都可以看到它)。

2)只能使用public作爲不可變的值。一個不可改變的值是不能改變的(所有成員都是最終的是確保這個最好的方法,或者在對象被構造並且變量都是私有的情況下,沒有方法修改任何值)。 3)儘可能總是聲明變量爲final(類變量,實例變量,參數,局部變量)。

這裏直接幫助你的提示是#3。由於您從未將值分配給「arr」,因此它爲空。如果你聲明它是最終的,那麼編譯器會強制你實際分配一個值,如果你沒有編譯代碼的話。

在開始編程時,做這件小事可以爲您節省數小時的時間。在我的情況下,我做了類似的事情,並不完全一樣(真的是我在一輪中違反了第二種方式),花了我大約一週的時間。我已經用Java編程超過15年了...如果我可以因爲這樣的事情而浪費一週的時間,想想你可以浪費多少時間:-)

相關問題