java.lang.NullPointerException 使用 switch case 时
java.lang.NullPointerException when using switch case
我在编译代码时得到一个 java.lang.NullPointerException,但我不知道为什么。
它说:
Exception in thread "main" java.lang.NullPointerException
at Beleg2/fantasyGame.Store.addGoods(Store.java:12)
at Beleg2/fantasyGame.Main.main(Main.java:19)
我想做的是建立三个商店:铁匠铺、珠宝商和书店,然后在每个商店中随机添加对象。例如,铁匠铺只有剑和弓。
我之前已经编程了一些非常相似的东西而没有那个问题。
主要
package fantasyGame;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Store<Weapon> smithy = new Store<>();
Store<Ring> jeweler = new Store<>();
Store<Scroll> bookstore = new Store<>();
Random zufall = new Random();
for(int j = 0; j < 12; j++) {
switch(zufall.nextInt(6)) {
case 0 : smithy.addGoods((Weapon)new Sword(150));
break;
case 1 : smithy.addGoods((Weapon)new Bow(100));
break;
case 2 : jeweler.addGoods((Ring)new Silverring(150));
break;
case 3 : jeweler.addGoods((Ring)new Goldring(300));
break;
case 4 : bookstore.addGoods((Scroll) new CurseOfTheQuillPen(500));
break;
case 5 : bookstore.addGoods( (Scroll)new TheEyesOfHypnos(500));
break;
default :
}
}
店铺class:
package fantasyGame;
import java.util.ArrayList;
public class Store <T extends Object>{
private ArrayList<T> goods;
private int inStore = 0;
public Store() {}
public void addGoods(T x) {
this.goods.add(x);
this.inStore++;
}
对象class:
package fantasyGame;
public class Object {
private int price;
public Object(int x) {
this.price = x;
}
private String specialSkill;
public void setSpecialSkill(String s) {
this.specialSkill = s;
}
武器class:(这只是一个例子,另外两个class戒指和卷轴看起来很像)
package fantasyGame;
public class Weapon extends Object {
public Weapon (int x) {
super(x);
};
}
剑 class:(其他 class 看起来非常相似只是有不同的特殊技能)
package fantasyGame;
public class Sword extends Weapon implements ForSaleIF {
public Sword(int x) {
super(x);
this.setSpecialSkill("The Amount of times this sword will hit: ");
};
非常感谢您的帮助!
在您的Store
class 中商品数组未初始化。如果将其更改为
它将起作用
private ArrayList<T> goods = new ArrayList<>();
我在编译代码时得到一个 java.lang.NullPointerException,但我不知道为什么。 它说:
Exception in thread "main" java.lang.NullPointerException
at Beleg2/fantasyGame.Store.addGoods(Store.java:12)
at Beleg2/fantasyGame.Main.main(Main.java:19)
我想做的是建立三个商店:铁匠铺、珠宝商和书店,然后在每个商店中随机添加对象。例如,铁匠铺只有剑和弓。 我之前已经编程了一些非常相似的东西而没有那个问题。 主要
package fantasyGame;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Store<Weapon> smithy = new Store<>();
Store<Ring> jeweler = new Store<>();
Store<Scroll> bookstore = new Store<>();
Random zufall = new Random();
for(int j = 0; j < 12; j++) {
switch(zufall.nextInt(6)) {
case 0 : smithy.addGoods((Weapon)new Sword(150));
break;
case 1 : smithy.addGoods((Weapon)new Bow(100));
break;
case 2 : jeweler.addGoods((Ring)new Silverring(150));
break;
case 3 : jeweler.addGoods((Ring)new Goldring(300));
break;
case 4 : bookstore.addGoods((Scroll) new CurseOfTheQuillPen(500));
break;
case 5 : bookstore.addGoods( (Scroll)new TheEyesOfHypnos(500));
break;
default :
}
}
店铺class:
package fantasyGame;
import java.util.ArrayList;
public class Store <T extends Object>{
private ArrayList<T> goods;
private int inStore = 0;
public Store() {}
public void addGoods(T x) {
this.goods.add(x);
this.inStore++;
}
对象class:
package fantasyGame;
public class Object {
private int price;
public Object(int x) {
this.price = x;
}
private String specialSkill;
public void setSpecialSkill(String s) {
this.specialSkill = s;
}
武器class:(这只是一个例子,另外两个class戒指和卷轴看起来很像)
package fantasyGame;
public class Weapon extends Object {
public Weapon (int x) {
super(x);
};
}
剑 class:(其他 class 看起来非常相似只是有不同的特殊技能)
package fantasyGame;
public class Sword extends Weapon implements ForSaleIF {
public Sword(int x) {
super(x);
this.setSpecialSkill("The Amount of times this sword will hit: ");
};
非常感谢您的帮助!
在您的Store
class 中商品数组未初始化。如果将其更改为
private ArrayList<T> goods = new ArrayList<>();