Java NullPointerException
I tried printStackTrace and I have coverted everything to static (I think)... however, lines 17 and line 38 are the problem... because of this error:
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 . . .
Line 17: anItem.addInv(1);
Line 38: arr.add("Dan");
And here is my code:
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";
开发者_如何学Python 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};
}
}
As you can see, it's those lines and I don't know what to do... :\
When you call the add() method on arr, it's not been initialized yet, hence, the NullPointerException.
Since you'll probably use the ArrayList in other methods as well, you should have that initialized in the constructor; ie:
public item() {
arr = new ArrayList<String>();
}
Variable arr is not initialized.
Variable arr in main() is not the same arr in function addInv()
Just initialize it in addInv to fix it.
String canEat = "false";
Why are you converting to and from strings?
You seem to have muddled an item
class an inventory
class.
Perhaps an Enum would be better:
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);
}
}
Then you can just have List<InventoryItem> inventory = new ArrayList<InventoryItem>()
within your player's class, and directly interface with that.
A few tips (one that does directly solve the problem):
1) wherever possible declare variables as private, or at most protected. I personally never use the "default" which is package level access (anything in the same package can see it).
2) Only use public for immutable values. An immutable value is something that cannot be changed (all members are final is the best way to ensure that, or no method modifies any values after the object is constructed and the variables are all private).
3) whenever possible always declare variables to be final (class variables, instance variables, parameters, local variables).
The one tip that directly helps you here is #3. Since you never assigned a value to "arr" it is null. If you declared it as final the compiler would force you do actually assign it a value, if you do not the code won't compile.
Doing that little thing will save you hours of time as you start programming. In my case I did something similar, not exactly the same (really I violated #2 sort of in a round about way) and it cost me about a week. I have been programming in Java for over 15 years now... if I can waste a week because of something like this think of how much time you can waste :-)
精彩评论