NullPointerException help

Discussion in 'Plugin Development' started by BlueBearPvP, Apr 15, 2016.

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

    BlueBearPvP

    Hey Guys,

    So I am creating a custom sword that when you hold it, it will gave you blindness. When I held the sword I get the blindness but when I switch to another item it still gave me it. It throws an error in the chat, saying that there is a Null Pointer but I dont know how to fix it. Thanks :D

    Here is the Main Code:

    Code:
    @EventHandler
        public void onTitanic(PlayerInteractEvent e) {
            final HumanEntity p = e.getPlayer();
            Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin)this, new Runnable(){
               
                @Override
                public void run() {
                    if (p.getItemInHand() != null) {           
                     if (p.getItemInHand().getType().equals(Material.DIAMOND_SWORD)
                             && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Titanic Blade") && !BearUHCRecipes.resistancePlayers.contains(p.getName())) {
                         BearUHCRecipes.resistancePlayers.add((Player)p);                
                     }   
                    } else if (BearUHCRecipes.resistancePlayers.contains((Object)p)) {
                            BearUHCRecipes.resistancePlayers.remove((Object)p);   
                    
                    }
                }
            }, 15);
        }
    Checker:

    Code:
    package com.bluebearpvp.recipes.TitanicCheck;
    
    import java.util.ArrayList;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.bluebearpvp.recipes.BearUHCRecipes;
    
    public class Titanic extends BukkitRunnable {
        private final JavaPlugin plugin;
        ArrayList<Player> resistancePlayers = BearUHCRecipes.resistancePlayers;
       
        public Titanic(JavaPlugin plugin) {
            this.plugin = plugin;
        }
       
        public void run() {
            for (Player p : this.resistancePlayers) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 25, 1), true);
            }
        }
    
    }
    
     
  2. Offline

    Gonmarte

    https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/
    Troubleshoot your own stactrace.
     
  3. Offline

    BlueBearPvP

    So I look into the error:

    Code:
    [20:03:07 WARN]: [BearUhcRecipes] Task #78 for BearUhcRecipes v1.2 generated an exception
    java.lang.NullPointerException
        at com.bluebearpvp.recipes.BearUHCRecipes$1.run(BearUHCRecipes.java:258) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:349) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:680) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_73]
    So there is a NullPointer on my code in line: 258 but I still dont know how to fix it.

    Code:
    && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Titanic Blade") && !BearUHCRecipes.resistancePlayers.contains(p.getName())) {
     
  4. Offline

    Zombie_Striker

    The following are all the values that can be null. Go through each value an figure out which one is null.
     
    clapynick likes this.
  5. Offline

    BlueBearPvP

    @Zombie_Striker

    So I think I figure out:

    Code:
    @Override
                public void run() {
                    if (p.getItemInHand() != null) {          
                     if (p.getItemInHand().getType().equals(Material.DIAMOND_SWORD) && p.getInventory().getItemInHand().hasItemMeta()
                             && p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Titanic Blade") && !BearUHCRecipes.resistancePlayers.contains(p.getName())) {
                         BearUHCRecipes.resistancePlayers.add((Player)p);               
                     }  
                    } else if (BearUHCRecipes.resistancePlayers.contains((Object)p)) {
                            BearUHCRecipes.resistancePlayers.remove((Object)p);  
                   
                    }
                }
            }, 15);
        }
    This is my new code ^

    Now, the console doesnt throw the error but it still wont work. When I held the titanic blade it give me blindness but when I switch to another item, it still give me blindness.
     
  6. Offline

    Zombie_Striker

    Look at what you wrote. You only remove the potion if the player has nothing in his hand. If he has anything else, it does not remove the player.
     
  7. Offline

    BlueBearPvP

    @Zombie_Striker

    Thanks so much! I figure it out! So I have another question.. When I switch to my titanic blade, it gives me blindness 4 sec later after I switch to it. Do I have to reduce the delay time to make that faster? That is also the same when I switch to another item, it takes the blindness off 4 sec later. Thanks!

    Here is the code after I fix it:

    Code:
    @EventHandler
        public void onTitanic(PlayerInteractEvent e) {
            final HumanEntity p = e.getPlayer();
            Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin)this, new Runnable(){
               
                @Override
                public void run() {           
                    if (p.getItemInHand().getType().equals(Material.DIAMOND_SWORD) && p.getInventory().getItemInHand().hasItemMeta()
                             && p.getItemInHand().getItemMeta().getDisplayName().equals((Object)ChatColor.RED + "Titanic Blade") && !BearUHCRecipes.resistancePlayers.contains(p.getName())) {
                         BearUHCRecipes.resistancePlayers.add((Player)p);                
                       
                    } else if (BearUHCRecipes.resistancePlayers.contains((Object)p)) {
                            BearUHCRecipes.resistancePlayers.remove((Object)p);   
                    }
                   
                }
            }, 15);
        }
     
  8. Offline

    Zombie_Striker

    @BlueBearPvP
    This value right here represents the delay after the player swaps an item. You do not want any delay, right? If so, just set the delay to 0.
     
  9. Offline

    BlueBearPvP

    Oh Ok, Thanks for all the help. I was just wanting to make sure. I didnt want to lag the server or mess something up.
     
  10. Offline

    BlueBearPvP

    @Zombie_Striker

    Thanks for the help so far. I have another question. So when I switch to the Titanic Blade, I have to swing it a couple of times in order for the blindness to start. So is there another event that tells when I switch to it, so that I dont have to swing in order for the blindess to start. This also goes for when I switch to another item, I have to swing a couple of times to get the blindess off. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page