Solved NullPointerException when getting Content of inventory

Discussion in 'Plugin Development' started by Julian1Reinhardt, Sep 28, 2013.

Thread Status:
Not open for further replies.
  1. It throws a NPE when I run this line of code:
    Code:
    ItemStack[] contents = plugin.generalInventory.getContents();
    This error log:
    Code:
    13:26:56 [SEVERE] Could not pass event InventoryCloseEvent to General v
    1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.handleInventor
    yCloseEvent(CraftEventFactory.java:684)
            at net.minecraft.server.v1_6_R2.PlayerConnection.handleContainerClose(Pl
    ayerConnection.java:1166)
            at net.minecraft.server.v1_6_R2.Packet101CloseWindow.handle(SourceFile:1
    7)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
            at me.vir.general.listener.inventoryClose(listener.java
    :29)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 15 more
     
  2. not all spaces inside the array of item stack contains item stacks, they may also include some null's if the spot inside the inventory is empty
     
  3. So i have to make it an array list or somehting else?
     
  4. If the line you provided is really the line with the NPE, you forgot to either initialize plugin or generalInventory. Check them to be correctly initialized. If you don't now what that means show more code please.
     
  5. hapm
    Plugin is initialized by this:
    Code:
    public main plugin;
        public listener (main a)
        {
            this.plugin = a;
        }
    And generalInventory is this:
    Code:
    Inventory generalInventory = Bukkit.createInventory(p, 54, "GEneral Inventory");
    This inventory should be accessable by every player, that's why the inventory owner is p.
     
  6. There can't be a NPE in the given line, when both variables are initialized. You're showing the wrong line, or one of these isn't initialized when the code gets executed.
     
  7. hapm
    But that is line 29 in my listener.java

    hapm
    Here is the whole listener.java
    http://pastebin.com/sefmhRwD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Show us the complete listener.java please, and be sure that the version of your code is identical to the one executed by bukkit.
    EDIT: ninja'ed

    Now show the method that initializes generalInventory in your main class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. You declare a new local variable named generalInventory that shadows and therefor hide the class field generalInventory. All changes to this variable will be lost at the end of the method and main.generalInventory will never be changed at all. Don't specify a type before generalInventory to get the class field:
    Code:java
    1. generalInventory = Bukkit.createInventory(p, 54, "General Inventory");
     
    Julian1Reinhardt likes this.
  10. hapm
    Thank you so much! I spent hours trying to figure out what the problem was
     
Thread Status:
Not open for further replies.

Share This Page