Solved Problems with inventory creation

Discussion in 'Plugin Development' started by Scrapnix, Feb 5, 2017.

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

    Scrapnix

    Hey, I want to create a new inventory with some items in it, but nothing happens.
    The error report says that it is a null pointer exception.
    My code is:
    Code:
    Inventory inv1 = null;
    
    @EventHandler
        public void onTeleClick1(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if (p.getWorld().getName().equals("Hub")) {
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
            if (p.getItemInHand().getType() == Material.BLAZE_ROD){
            p.playSound(p.getLocation(), Sound.LEVEL_UP, 1F, 1F);
            inv1 = p.getServer().createInventory(null, 9, "§6§l>>§a§lSichtbarkeit");
            ItemStack T = new ItemStack(Material.STAINED_GLASS_PANE);
            T.setDurability((short) 7);
            ItemStack yes = new ItemStack(Material.EMERALD_BLOCK);
            ItemStack no = new ItemStack(Material.REDSTONE_BLOCK);
            ItemMeta wm1 = yes.getItemMeta();
            ItemMeta wm2 = no.getItemMeta();
            wm1.setDisplayName("Spieler anzeigen");
            wm2.setDisplayName("Spieler verbergen");
            yes.setItemMeta(wm1);
            no.setItemMeta(wm2);
           
            inv.setItem(0, T);
            inv.setItem(1, T);
            inv.setItem(2, yes);
            inv.setItem(3, T);
            inv.setItem(4, T);
            inv.setItem(5, T);
            inv.setItem(6, no);
            inv.setItem(7, T);
            inv.setItem(8, T);
            p.openInventory(inv1);
        }
            }
            }
        }
     
  2. Offline

    mehboss

    @Scrapnix
    We need the full error.


    Sent from my iPhone using Tapatalk
     
    Rayzr522 likes this.
  3. Offline

    Scrapnix

    This is the full piece of error:
    HTML:
    [02:13:26] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to BlockBreak v1.0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_111]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
        at com.BlockBreak.Main.onTeleClick1(Main.java:340) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 17 more
    
     
  4. Offline

    mehboss

    @Scrapnix
    Why are you setting the variable to null on the top?


    Sent from my iPhone using Tapatalk
     
    Rayzr522 likes this.
  5. Offline

    Scrapnix

    Ouu, you're right, that is unnecessary, but that shouldn't cause the errors. I will try it later :)

    Gesendet von meinem LG-H420 mit Tapatalk
     
  6. Offline

    mehboss

    @Scrapnix
    Alright

    Also, I don't think that is the problem either but can you tell me what line 340 is. That is what is causing the null pointer.
     
    Last edited: Feb 5, 2017
  7. Offline

    Drkmaster83

    @Scrapnix
    Code:
    if (p.getItemInHand().getType() == Material.BLAZE_ROD){
    
    You have to check whether they're holding anything at all or if it's air before you do that.
     
  8. Offline

    Disgastings

    I'd recommend creating a listener class for PlayerInteractEvent.
     
  9. Offline

    Rayzr522

    You have to first make sure that p.getItemInHand() isn't null, sometimes it is if they're holding nothing in their hand. So if it is null, calling .getType() on it is what's causing your NPE. Just first make sure p.getItemInHand() != null ;)

    Also please please please use indentation in your code, especially if you expect other people to read and understand it. Please.

    EDIT: Looking at your code again, you're storing a single instance of inv1? Why? If you only ever need a single instance, just create the inventory once in the constructor of your class and don't waste CPU and memory recreating it each time. I mean, it's identical each time. It's literally a yes/no dialogue.

    EDIT 2: Sorry, I'm not trying to be rude, I'm just a very blunt person. I'm only trying to help :)
     
    Scrapnix, Zombie_Striker and mehboss like this.
  10. Offline

    Scrapnix

    Hey, it was my mistake, I raplaced inv1 in the .setItem() method with inv which is another inventory in my plugin :) Thanks to your help.

    Gesendet von meinem LG-H420 mit Tapatalk
     
  11. Offline

    Zombie_Striker

    @Scrapnix
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page