所以这是我对构造函数的第一次体验,我似乎在我的 getter 方法中遇到了一个空指针异常。
具有能力构造函数的类
public class Ability {
private String name;
private String lore;
private ItemStack is;
private Material m;
public Ability(Material m, String name, String lore){
this.name = name;
this.lore = lore;
this.m = m;
this.is = new ItemStack(m);
String[] aLore = new String[1];
aLore[0] = lore;
ItemMeta im = is.getItemMeta();
im.setDisplayName(name);
im.setLore(Arrays.asList(aLore));
is.setItemMeta(im);
}
public ItemStack getItem(){
return is;
}
@EventHandler(priority=EventPriority.HIGH)
public void onPlayerUse(PlayerInteractEvent event){
Player p = event.getPlayer();
is = p.getItemInHand();
}
具有能力构造函数实例的类:
public class AbilityManager {
public Ability strafeRun;
ThunderItems ti = new ThunderItems();
Inventory toolbar;
ItemStack[] contents = new ItemStack[9];
public void loadAbilityBar(Player p){
if(ThunderItems.perms.playerInGroup(p, "Scout")){
strafeRun = new Ability(Material.TNT, "Strafe Run", "Test Lore");
contents[0] = strafeRun.getItem();
}
toolbar = Bukkit.createInventory(p, 9, ChatColor.DARK_BLUE + "Abilities");
toolbar.addItem(contents[0]);
p.openInventory(toolbar);
}
}
}
堆栈跟踪:
org.bukkit.command.CommandException: Unhandled exception executing command 'a' in plugin ThunderItems v0.1
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
Caused by: java.lang.IllegalArgumentException: Item cannot be null
at org.apache.commons.lang.Validate.noNullElements(Validate.java:410)
at org.bukkit.craftbukkit.v1_6_R3.inventory.CraftInventory.addItem(CraftInventory.java:268)
at com.r0llingthund3r.abilities.AbilityManager.loadAbilityBar(AbilityManager.java:27)
at com.r0llingthund3r.main.ThunderItemsCommandExecutor.onCommand(ThunderItemsCommandExecutor.java:36)
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
我认为我没有遗漏任何东西。你们中的一些人可能会认为这是 Minecraft 的 BukkitAPI。