为什么我的构造函数中的getter返回null?

时间:2013-09-29 19:51:23

标签: java constructor getter

所以这是我第一次使用构造函数,我似乎在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();
}

具有Ability构造函数实例的类:

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)

我认为我没有留下任何东西。你们当中有些人可能认为这是我的世界的BukkitAPI。

0 个答案:

没有答案