0

So this is my one of my first experiences with constructors, and I seem to be getting a null pointer exception in my getter method.

Class with Ability Constructor

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();
}

Class with instance of Ability constructor:

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);
    }
  }
}

Stack Trace:

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)

I don't think I left anything out. Some of you may recognize this as the BukkitAPI for Minecraft.

3
  • I think we need to see the loadAbilityBar method from AbilityManager to say what's going on here. And why do you think that a constructor is broken? Your stacktrace doesn't point to a constructor at all ...
    – jacobm
    Sep 29, 2013 at 20:22
  • You left out most of your stacktrace so it's kind of hard to tell where this is coming from. Sep 29, 2013 at 20:22
  • loadAbilityBar is already included in the code. Not sure what you are referring to jacobm. I added the full stack trace though. Sep 29, 2013 at 20:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.