Mysterious NPE

Discussion in 'Plugin Development' started by user_91277742, May 29, 2018.

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

    user_91277742

    Hello there!
    So.. I finished my core plugin of my server. Its just a race plugin with others stuff.. The problem its when i do a left-click outside of a random inventory.. Appear this error
    Code:
    [04:04:17 ERROR]: Could not pass event InventoryClickEvent to RazasLFA v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1889) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
            at io.github.amaknajaja.Main.Eventos.seleccionRaza(Eventos.java:35) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            ... 15 more
    
    the real poblem is... I dont really understand ,why this line has an NPE.. I will show you my code
    Code:
        public void race(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            if(e.getCurrentItem() != null || e.getCurrentItem().getType() != Material.AIR){
                ItemStack clicked = e.getCurrentItem();
                if(clicked != null) {
                    if(clicked.hasItemMeta()) {
                             // stuff
    
    Eventos.java:35 = ( ItemStack clicked = e.getCurrentItem(); )
     
    Last edited by a moderator: May 29, 2018
  2. Offline

    Zombie_Striker

    @Ahrigumi
    The || Means you are checking if it is not null , or if it is null, check if the null object's type is not air. I think you meant to use &&.
     
  3. Offline

    user_91277742

    Hello, thanks for your answer. I tried to change || to &&, but i have the same :C . I tried to check if my left-click is null in differents ways but nothing
     
  4. Offline

    Zombie_Striker

    @Ahrigumi
    Try moving the second statement into another line below the first if. If that does not fix the problem, post the full class with the updated error.
     
  5. Offline

    user_91277742

    @Zombie_Striker Nothing :/
    Code:
    package io.github.amaknajaja.Main;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Eventos implements Listener {
    
        static Razas razas;
    
        public Eventos(Razas razasClass){
            razas = razasClass;
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void seleccionRaza(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            ItemStack clicked = e.getCurrentItem();
            if(clicked != null) {    <-- Line 35
                if(e.getCurrentItem() != null && e.getCurrentItem().getType() != Material.AIR){
                    if(clicked.hasItemMeta()) {
                        ItemMeta check = e.getCurrentItem().getItemMeta();
                        String checkName = ChatColor.stripColor(check.getDisplayName());
                        String raza = razas.razaConfig.getString(p.getUniqueId().toString() + ".Raza");
                        if(e.getView().getTopInventory().getTitle().equals(ChatColor.AQUA + "Selecciona tu raza")){
                            e.setCancelled(true);
                            if(clicked.getType() == Material.APPLE) {
                                if(!(checkName.equals(raza))) {
                                    razas.ec.withdrawPlayer(p.getName(), razas.getConfig().getDouble("precio"));
                                    razas.razaConfig.set(p.getUniqueId().toString() + ".Raza","Nekos");
                                    razas.razaConfig.set(p.getUniqueId().toString() + ".Nombre", p.getName());
                                    razas.guardarConfiguracion();
                                    p.getInventory().setHelmet(razas.orejitasLista("Nekos"));
                                    p.sendMessage(razas.color(razas.getConfig().getString("neko-message")));
                                    p.closeInventory();
                                }else {
                                    p.sendMessage(ChatColor.RED + "Ya perteneces a esta raza!");
                                    p.closeInventory();
                                }
                           }
                        }
                     }
                 }
             }
         }
    }
    
    Error
    Code:
    [05:58:19 ERROR]: Could not pass event InventoryClickEvent to RazasLFA v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1889) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
            at io.github.amaknajaja.Main.Eventos.seleccionRaza(Eventos.java:35) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
            at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-2086bb0-8cc5a7e]
            ... 15 more
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi Add debug statments to see where the code is getting.
    I personally think that it is the line with getDisplayName
     
  7. Offline

    user_91277742

    @timtower
    I tried the same code but in a different plugin and i have the same error in the same line
    Code:
        @EventHandler
        public void banco(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            ItemStack clicked = e.getCurrentItem();
            if(clicked.hasItemMeta() && clicked != null) {     <------ 34
                if(e.getView().getTopInventory().getTitle().equals(ChatColor.GREEN + "Banco Nairiano")){
                    e.setCancelled(true);
                    if(clicked != null) {
                        if(clicked.getType() == Material.BOOK_AND_QUILL) {
                            if(!(main.cuentasConfig.contains(p.getUniqueId().toString()))){
                                p.closeInventory();
                                p.openInventory(invs.chooseCreate());
                            }else {
                                p.closeInventory();
                                p.sendMessage(ChatColor.RED + "You have already an account!");
                            }
                        }
                    }
    
                }
            }
        }
    
    The error in the line 34

    Its strange because all works perfectly, but.. i have this issue in cmd all the time when you do a left-click randomdly outside of an inventory
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Ahrigumi With the second plugin it can be the check order.
    But please add print statements to see where it is getting.
     
  9. Offline

    user_91277742

    @timtower
    Im trying to put something like this "System.out.println(clicked)" to "debug" this statement but just appear the error.
    Im sorry i dont know other way to debug
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    Artellet

    You're checking if the item isn't null in all the wrong places.
    Try something like this instead:
    Code:
        @EventHandler
        public void banco(InventoryClickEvent e) {
            ItemStack clicked = e.getCurrentItem();
            if (clicked == null) {
                return;
            }
            Player p = (Player) e.getWhoClicked();
            if (clicked.hasItemMeta()) {
                if (e.getView().getTopInventory().getTitle().equals(ChatColor.GREEN + "Banco Nairiano")) {
                    e.setCancelled(true);
                    if (clicked.getType() == Material.BOOK_AND_QUILL) {
                        if (!(main.cuentasConfig.contains(p.getUniqueId().toString()))) {
                            p.closeInventory();
                            p.openInventory(invs.chooseCreate());
                        } else {
                            p.closeInventory();
                            p.sendMessage(ChatColor.RED + "You have already an account!");
                        }
                    }
                }
            }
        }
    
    Big emphasis on this bit:
    Code:
            ItemStack clicked = e.getCurrentItem();
            if (clicked == null) {
                return;
            }
     
    Last edited: May 30, 2018
Thread Status:
Not open for further replies.

Share This Page