Solved yet another NullPointer

Discussion in 'Plugin Development' started by SM3XY_jAMES, Apr 2, 2019.

  1. Offline

    SM3XY_jAMES

    Code:
    package net.JameZ.listeners;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    import net.JameZ.Cucumber;
    import net.JameZ.data._File;
    import net.JameZ.data._Player;
    import net.JameZ.data._Server;
    import net.JameZ.files.getPlayerFile;
    
    public class onDeath implements Listener
    {
        private Cucumber plugin;
       
      public onDeath(Cucumber plugin)
      {
        this.plugin = plugin; 
      }
     
    @EventHandler
      public void onPlayerDeath(PlayerDeathEvent e)
      {
        Player killer = e.getEntity().getKiller();
        Player target = e.getEntity();
       
        _File fileK = getPlayerFile.getFile(killer);
        _File fileD = getPlayerFile.getFile(target);
       
        if (e.getEntity().getKiller() instanceof Player)
        {       
           
            fileK.increment("stats.kills");       
            fileD.increment("stats.deaths");   
           
            e.setDeathMessage(null);
           
            String itemName = "";
           
            if (killer.getInventory().getItemInMainHand().getType().equals(Material.AIR) || killer.getInventory().getType() == null)
            {
                itemName = "there fists.";
            }
            else
            {
                itemName = killer.getInventory().getItemInHand().getItemMeta().getDisplayName();
                // where the error lies ^ (line 52)
            }
           
            _Server.broadcastMessage(this.plugin.getMessage(true, "player.death.by-player").replaceAll("%item%", itemName).replaceAll("%k_player%", _Player.getDisplayname(target)).replaceAll("%d_player%", _Player.getDisplayname(killer)));
        }
        else
        {
            fileD.increment("stats.deaths");   
            e.setDeathMessage(null);
        }
      }
    }
    
    Code:
    [01:00:37 ERROR]: Could not pass event PlayerDeathEvent to Cucumber v2.0.1.5
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:413) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityPlayer.die(EntityPlayer.java:432) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityLiving.damageEntity(EntityLiving.java:956) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityHuman.damageEntity(EntityHuman.java:759) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityPlayer.damageEntity(EntityPlayer.java:586) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityHuman.attack(EntityHuman.java:1005) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.EntityPlayer.attack(EntityPlayer.java:1311) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1623) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseEntity.a(SourceFile:69) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PacketPlayInUseEntity.a(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at java.util.regex.Matcher.appendReplacement(Unknown Source) ~[?:1.8.0_161]
            at java.util.regex.Matcher.replaceAll(Unknown Source) ~[?:1.8.0_161]
            at java.lang.String.replaceAll(Unknown Source) ~[?:1.8.0_161]
            at net.JameZ.listeners.onDeath.onPlayerDeath(onDeath.java:52) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            ... 22 more
    So I am having a issue where im trying to make a custom death message where it also displays the item used but I've done searching and couldn't find anything as to why its returning Null.
     
  2. Offline

    timtower Ninja on the waves Moderator

    @SM3XY_jAMES Check if is a player before you are using it as one.
    And there might not be a displayname for the item
     
  3. Offline

    SM3XY_jAMES

    @timtower is did that once

    Code:
    if (e.getEntity().getKiller() instanceof Player)
    {
    where should i put it again.
     
  4. Offline

    KarimAKL

    @SM3XY_jAMES You only need to check if it's a player once. You just need to do 'Player killer = target.getKiller()' after you've checked.
     
  5. Offline

    Tango_

    Try changing .replaceAll() to just .replace()
     
  6. Offline

    SM3XY_jAMES

    made a few changes but the error still lies @Tango_ @KarimAKL @timtower

    Code:
    package net.JameZ.listeners;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    import net.JameZ.Cucumber;
    import net.JameZ.data._File;
    import net.JameZ.data._Player;
    import net.JameZ.data._Server;
    import net.JameZ.files.getPlayerFile;
    
    public class onDeath implements Listener
    {
        private Cucumber plugin;
     
      public onDeath(Cucumber plugin)
      {
        this.plugin = plugin;
      }
    
    @SuppressWarnings("deprecation")
    @EventHandler
      public void onPlayerDeath(PlayerDeathEvent e)
      {
    
        if (e.getEntity().getKiller() instanceof Player)
        {     
            e.setDeathMessage(null);
         
            Player target = e.getEntity();
            Player killer = target.getKiller();
         
            _File fileK = getPlayerFile.getFile(killer);
            _File fileD = getPlayerFile.getFile(target);
         
            String itemName = "";
    
            fileK.increment("stats.kills");     
            fileD.increment("stats.deaths"); 
                 
            if (killer.getInventory().getItemInHand().getType().equals(Material.AIR) || killer.getInventory().getType() == null)
            {
                itemName = "there fists.";
            }
            else
            {
                itemName = killer.getInventory().getItemInHand().getItemMeta().getDisplayName();
            }
         
            _Server.broadcastMessage(this.plugin.getMessage(true, "player.death.by-player")
                    .replace("%item%", itemName) // <error NullPointer
                    .replaceAll("%k_player%", _Player.getDisplayname(target))
                    .replaceAll("%d_player%", _Player.getDisplayname(killer))
                    );     
        }
        else
        {
           //   fileD.increment("stats.deaths"); 
        }
      }
    }
    
     
  7. Offline

    timtower Ninja on the waves Moderator

    @SM3XY_jAMES Item can be null,
    killer.getInventory().getItemInHand().getItemMeta().getDisplayName() can return null
     
  8. Offline

    SM3XY_jAMES

    @timtower that was it; thank you it works now.
     

Share This Page