我为Minecraft 1.8.9创建了自己的ItemBuilder类。如果现在尝试使用addLore("Test")
,则行lore.add(s);
会出现空指针异常。我不知道问题是什么或如何解决。
public class ItemBuilder{
Material material;
int amount = 1;
short damage = 0;
Byte subid = 0;
List<String> lore;
String name = "";
//builder
public ItemBuilder(Material material){
this.material = material;
}
public ItemBuilder(Material material, Byte subid){
this.material = material;
this.subid = subid;
}
public ItemBuilder setDamage(int damage){
this.damage = (short) damage;
return this;
}
public ItemBuilder setLore(List<String> lore){
this.lore = lore;
return this;
}
public ItemBuilder addLore(String s){
List<String> lore = this.lore;
lore.add(s);
this.lore = lore;
return this;
}
public ItemBuilder setDisplayName(String s){
this.name = s;
return this;
}
public ItemStack build(){
ItemStack itemStack = new ItemStack(this.material, this.amount, this.damage, this.subid);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(this.name);
itemMeta.setLore(this.lore);
itemStack.setItemMeta(itemMeta);
return itemStack;
}
}
哦,是的,已经解决了。
我只需要更改清单知识;至列表知识=新的LinkedList <>();