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

    r0llingthund3r

    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.

    Code:java
    1. public class Ability {
    2. String name;
    3. String lore;
    4. Material m;
    5. ItemStack is;
    6.  
    7. public Ability(Material m, String name, String lore){
    8. this.name = name;
    9. this.lore = lore;
    10. this.m = m;
    11. this.is = 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. }
    19.  
    20. public ItemStack getItem(){
    21. return is;
    22. }


    Code:java
    1. public class AbilityManager {
    2. public Ability strafeRun;
    3. ThunderItems ti = new ThunderItems();
    4. Inventory toolbar;
    5. ItemStack[] contents = new ItemStack[9];
    6.  
    7.  
    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. }


    Code:
    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(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.NullPointerException
        at com.r0llingthund3r.abilities.AbilityManager.loadAbilityBar(AbilityManager.java:16)
        at com.r0llingthund3r.main.ThunderItemsCommandExecutor.onCommand(ThunderItemsCommandExecutor.java:36)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    
    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

    remremrem

    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

    r0llingthund3r

    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