Getting NPE on inventory.addItem() usage

Discussion in 'Plugin Development' started by r0llingthund3r, Oct 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    So this is my first time working with constructors (definitely still a java amateur) and I think I am structuring it wrong. I have created an object called Ability and in the other class I'm trying to retrieve the itemstack with the metadata to add to the inventory toolbar. I am getting an NPE.

    1. public class Ability {
    2. String name;
    3. String lore;
    4. Material m;
    5. ItemStack is;
    7. public Ability(Material m, String name, String lore){
    8. = name;
    9. this.lore = lore;
    10. this.m = m;
    11. = new ItemStack(m, 1);
    12. String[] aLore = new String[1];
    13. aLore[0] = lore;
    14. ItemMeta im = is.getItemMeta();
    15. im.setDisplayName(name);
    16. im.setLore(Arrays.asList(aLore));
    17. is.setItemMeta(im);
    18. }
    20. public ItemStack getItem(){
    21. return is;
    22. }

    1. public class AbilityManager {
    2. public Ability strafeRun;
    3. ThunderItems ti = new ThunderItems();
    4. Inventory toolbar;
    5. ItemStack[] contents = new ItemStack[9];
    8. public void loadAbilityBar(Player p){
    9. if(ThunderItems.perms.playerInGroup(p, "Scout")){
    10. strafeRun = new Ability(Material.TNT, "Strafe Run", "Test Lore");
    11. ti.getLogger().info(contents[0].getType().toString());
    12. ItemStack test = new ItemStack(Material.TNT);
    13. contents[0] = test;//strafeRun.getItem();
    14. }
    15. toolbar = Bukkit.createInventory(p, 9, ChatColor.DARK_BLUE + "Abilities");
    16. toolbar.addItem(strafeRun.getItem());
    17. //working toolbar.addItem(new ItemStack(Material.APPLE, 1));
    18. p.openInventory(toolbar);
    19. }

    23:14:34 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'abilities' in plugin ThunderItems v0.1
        at org.bukkit.command.PluginCommand.execute(
        at org.bukkit.command.SimpleCommandMap.dispatch(
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(
        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(
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(
    Caused by: java.lang.NullPointerException
        at com.r0llingthund3r.abilities.AbilityManager.loadAbilityBar(
        at com.r0llingthund3r.main.ThunderItemsCommandExecutor.onCommand(
        at org.bukkit.command.PluginCommand.execute(
    Is the problem in the way I'm retrieving the itemstack from my ability? I'm pretty lost.
    I think I've provided all of the necessary code. Hopefully someone can help me squash this bug.
  2. Offline


    Make sure the code inside the if statement on line 9 in AbilityManager is being executed.
    (i suggest sending a message to the console when that code is run)

    Otherwise strafeRun is still just null from when it was initialized on line 2.
  3. Offline


    I will try that thanks.

    EDIT- It would seem you are correct, the contents of that if statement isn't being executed. I'm still not sure why though, I am certain that I am in the correct group...
Thread Status:
Not open for further replies.

Share This Page