2010-07-17 90 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年了...如果我可以因为这样的事情而浪费一周的时间,想想你可以浪费多少时间:-)

相关问题