NPE with ItemStack

Discussion in 'Plugin Development' started by HeadGam3z, May 24, 2014.

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

    HeadGam3z

    Code:java
    1. public static final ItemStack getHelm() {
    2. ItemStack itemStack = new ItemStack(Material.CHAINMAIL_HELMET, 1);
    3. itemStack.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, plugin.getConfig().getInt("Armor_Protection")); // line 222
    4. itemStack.addUnsafeEnchantment(Enchantment.DURABILITY, plugin
    5. .getConfig().getInt("Armor_Unbreaking"));
    6. ItemMeta itemMeta = itemStack.getItemMeta();
    7. itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&',
    8. plugin.getConfig().getString("Helmet_Name")));
    9. itemStack.setItemMeta(itemMeta);
    10. return itemStack;
    11. }


    Code:
    Armor_Protection: 10
    Armor_Unbreaking: 10
    Helmet_Name: '&4Guards Helmet'
    Code:
    [22:07:57] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin fcGuardPlugin v0.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.NullPointerException
        at com.gmail.mcheadgam3z.Main.getHelm(Main.java:222) ~[?:?]
        at com.gmail.mcheadgam3z.Main.onCommand(Main.java:85) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
        ... 13 more
    
    I don't understand. Why is there a NPE error? It seems like it's okay.
     
  2. Offline

    xJeremyCx

    Post your full code please. Which is line 222?
     
  3. Offline

    Sabersamus

    Where is 'plugin' defined? Are you 100% sure plugin != null?
     
  4. Offline

    HeadGam3z

    xJeremyCx
    I added a comment on line 3 stating where line 222 is, scroll a little bit to the right to see.

    Sabersamus
    Yes, I'm sure.

    Edit:
    Sabersamus
    Code:
    static Main plugin;
    is plugin. right under my public class where it extends JavaPlugin.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    Garris0n

    "plugin" is null.
     
  6. Offline

    Sabersamus

    Okay, but is it ever given a value?

    With the information you're giving us, it seems Garris0n is correct.

    Unless you actually show us putting plugin = something, we must assume that plugin is null.
     
  7. Offline

    Garris0n

    There is no assumption, plugin is null.

    Without more code, I can't tell you why it's null, but it is null.
     
  8. Offline

    HeadGam3z

    Garris0n Sabersamus
    Then it is null, whoops.
    But, I used, "plugin.getConfig().getString("Server_Name"); // something else in the config.yml"
    while plugin is null and it worked. What should I do?
     
  9. Offline

    Garris0n

    We can't tell you that without more context.
     
  10. Offline

    Sabersamus



    Its possible that plugin isn't null, and in fact, the values in his config are null. If he used
    Code:
    plugin.getConfig().getString("whatever");
    and it gave him a value, plugin couldn't be null,

    but if he did
    Code:
    plugin.getConfig().getString("something_else");
    and got an NPE, its clear that the values are null in his config.


    HeadGam3z this is why its important that you give us all relative information.

    EDIT: WOW I'm stupid. I just looked at the first post and saw his config. Whoops.

    Yeah your plugin instance is null.
     
  11. Offline

    HeadGam3z

    Sabersamus
    So, to un-null it, what would I assign it to?
     
  12. Offline

    Sabersamus

    That depends. Are you using this in your main class? Or a separate class?

    If in your main class,

    Code:
    onEnable(){
       plugin = this;
    }
    separate class

    Code:
    constructor(Main instance){
        plugin = instance;
    }
     
  13. Offline

    Garris0n

    Yes, but that wouldn't cause the NPE on that specific line. It would be in the Bukkit method somewhere. Meaning the NPE is on the only value not coming from or going to Bukkit - plugin. I'm assuming nothing from Bukkit is null, of course, which should be valid unless he's doing some weird stuff.
     
Thread Status:
Not open for further replies.

Share This Page