Solved List<ItemStack>.toArray returning null

Discussion in 'Plugin Development' started by MrDaniel, Jan 15, 2020.

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

    MrDaniel

    Hello there,

    I've been trying to get a player's inventory from the config, but I keep getting a nullPointerException

    Saving the inventory:
    Code:
    info.set("vanish.inventories." + p.getUniqueId() + ".inventory", p.getInventory().getContents());
    
    info.set("vanish.inventories." + p.getUniqueId() + ".armor", p.getInventory().getArmorContents());
    
    ServerHandler.saveConfigs();
    
    p.getInventory().clear();
    Getting the inventory:
    Code:
    List<ItemStack> raw_inventory = (List<ItemStack>) ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".inventory");
    
    List<ItemStack> raw_armor = (List<ItemStack>) ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".armor");
    
    
    ItemStack[] inventory = raw_inventory.toArray(new ItemStack[0]);
    
    ItemStack[] armor = raw_armor.toArray(new ItemStack[0]);
    
    
    p.getInventory().setContents(inventory);
    
    p.getInventory().setArmorContents(armor);
    I keep getting a nullPointerException on line 92 ItemStack[] inventory = raw_inventory.toArray(new ItemStack[0]);

    Anyone know why?

    Log (open)
    [19:42:36] [Server thread/ERROR]: Could not pass event PlayerChatEvent to ServerArchitect v1.0

    org.bukkit.event.EventException: null

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at net.minecraft.server.v1_14_R1.PlayerConnection$4.evaluate(PlayerConnection.java:1572) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at org.bukkit.craftbukkit.v1_14_R1.util.Waitable.run(Waitable.java:25) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1035) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:393) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:970) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:815) [spigot.jar:git-Spigot-cbd1a1b-009d8af]

    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_231]

    Caused by: java.lang.NullPointerException

    at ladeira.vanish.VanishHandler.loadInventory(VanishHandler.java:92) ~[?:?]

    at ladeira.vanish.VanishHandler.unvanishPlayer(VanishHandler.java:66) ~[?:?]

    at ladeira.vanish.cVanish.onUnvanishCommand(cVanish.java:33) ~[?:?]

    at ladeira.events.ChatEvent.onPlayerChat(ChatEvent.java:30) ~[?:?]

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231]

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]

    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]

    ... 10 more


    Code:
    [19:42:36] [Server thread/ERROR]: Could not pass event PlayerChatEvent to ServerArchitect v1.0
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at net.minecraft.server.v1_14_R1.PlayerConnection$4.evaluate(PlayerConnection.java:1572) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at org.bukkit.craftbukkit.v1_14_R1.util.Waitable.run(Waitable.java:25) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:1035) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:393) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:970) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:815) [spigot.jar:git-Spigot-cbd1a1b-009d8af]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
        at ladeira.vanish.VanishHandler.loadInventory(VanishHandler.java:92) ~[?:?]
        at ladeira.vanish.VanishHandler.unvanishPlayer(VanishHandler.java:66) ~[?:?]
        at ladeira.vanish.cVanish.onUnvanishCommand(cVanish.java:33) ~[?:?]
        at ladeira.events.ChatEvent.onPlayerChat(ChatEvent.java:30) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot.jar:git-Spigot-cbd1a1b-009d8af]
        ... 10 more
    


    thx

    EDIT: After some debugging I found out that the null pointer exception also happens on the second conversion

    EDIT2: The raw_inventory is null, how can that happen?
     
    Last edited: Jan 15, 2020
  2. Offline

    zThana

    Hello there,

    When looking at these 2 lines, I could already imagine how many things in it could be null, which I'll leave up to you to figure out:
    Code:
    List<ItemStack> raw_inventory = (List<ItemStack>) ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".inventory");
    List<ItemStack> raw_armor = (List<ItemStack>) ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".armor");
    Everything that could possibly return null are:
    • ServerHandler.getPlugin()
    • ServerHandler.getPlugin().getConfig()
    • ServerHandler.getPlugin().getConfig().getList("vanish.")
    • ServerHandler.getPlugin().getConfig().getList("vanish.inventories")
    • ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".inventory")
    • ServerHandler.getPlugin().getConfig().getList("vanish.")
    • ServerHandler.getPlugin().getConfig().getList("vanish.inventories")
    • ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".armor")
    I'm honestly a little worried about you not using serialization and deserialization to save the ItemStacks.
    If someone's inventory is empty and they vanish, you'd basically be telling the plugin:
    Code:
    info.set("vanish.inventories." + p.getUniqueId() + ".inventory", null);
    Same goes for armor, of course, and you'd afterwards be setting their inventories to null as well.

    I'm also a little confused as to how you're storing an ItemStack[] but trying to get it back by forcing it to do so in an List<ItemStack> here:
    Code:
    List<ItemStack> raw_armor = (List<ItemStack>) ServerHandler.getPlugin().getConfig().getList("vanish.inventories." + p.getUniqueId() + ".armor");
    Give the link above a try, I'm sure you'll love it.
     
    MrDaniel likes this.
  3. Offline

    MrDaniel

Thread Status:
Not open for further replies.

Share This Page