Solved NullPointerException

Discussion in 'Plugin Development' started by Matroxko, Aug 30, 2019.

  1. Offline

    Matroxko

    Can someone help me. I get this error when comparing if Material is Compass.I checked if its null but it still gives nullpointerexception
    Code:
     [Server thread/WARN]: [CompassTracker] Task #5 for CompassTracker v1 generated an exception
    java.lang.NullPointerException: null
            at matroxko.compasstracker.com.Main$1.run(Main.java:89) ~[?:?]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftTask.run(CraftTask.java:77) ~[craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:393) [craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at net.minecraft.server.v1_14_R1.MinecraftServer.b(MinecraftServer.java:956) [craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:370) [craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:911) [craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:765) [craftbukkit-1.14.3.jar:git-Bukkit-f1f3355]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Code:
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
    
            new BukkitRunnable() {
    
                @Override
                public void run() {
                    if (player.getInventory().contains(Material.COMPASS)) {
                        for (ItemStack is : player.getInventory().getContents()) {
                            if (is.getType().equals(Material.COMPASS) && is != null && is.getType() != Material.AIR) {
                                if (getNearestPlayer(player) != null && getNearestPlayer(player) != player) {
    
                                    double dis = getNearestPlayer(player).getLocation().distance(player.getLocation());
                                    String name = getNearestPlayer(player).getName();
    
                                    ItemMeta meta = is.getItemMeta();
                                    meta.setDisplayName(Color(getConfig().getString("trackitem.name")
                                            .replaceAll("%player%", name).replaceAll("%distance%", dis + "")));
                                    is.setItemMeta(meta);
                                    player.setCompassTarget(getNearestPlayer(player).getLocation());
    
                                } else {
    
                                    ItemMeta meta = is.getItemMeta();
                                    meta.setDisplayName(Color(getConfig().getString("trackitem.name")
                                            .replaceAll("%player%", "---").replaceAll("%distance%", "---")));
                                    is.setItemMeta(meta);
                                }
                            }
                        }
                    }
                }
            }.runTaskTimer(this, 0, 5);
    
        }
    Line 89:
    Code:
     if (is.getType().equals(Material.COMPASS) && is != null && is.getType() != Material.AIR) {
    
     
  2. Offline

    CraftCreeper6

    @Matroxko
    Do the null check before you check the type.

    Code:
    if (is != null)
    {
         if(is.getType()...)
         {
     
    Matroxko likes this.
  3. Offline

    Matroxko

    Yeah, thanks. It worked :)
     

Share This Page