Null pointer on Death

Discussion in 'Plugin Development' started by arnie231, Sep 10, 2012.

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

    arnie231

    Heres the code & Error

    Code:java
    1. package com.arnie.sh2.Listeners;
    2.  
    3. import com.arnie.sh2.Main;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11.  
    12.  
    13. public class PlayerDeath implements Listener
    14. {
    15. @EventHandler(priority=EventPriority.NORMAL)
    16. public void DeathCharge(PlayerDeathEvent event){
    17. if (event.getEntity() instanceof Player){
    18. Player player = event.getEntity();
    19. if (player.hasPermission("Stronghold2.Death.Exempt")){
    20. return;
    21. }
    22. if (Main.Config.getBoolean("Stronghold2.Economy.Enabled")){
    23. double amount = Main.Config.getLong("Stronghold2.Death.Price");
    24. EconomyResponse er = Main.economy.withdrawPlayer(player.getName(), amount);
    25. if (er.transactionSuccess()){
    26. player.sendMessage(ChatColor.GOLD + "[Stronghold2]" + " " + ChatColor.RED + "You lost" + " " + ChatColor.GREEN + amount + " " + "for Dieing!");
    27. }else{
    28. player.sendMessage(ChatColor.GOLD + "[Stronghold2]" + " " + ChatColor.DARK_RED + "An error occured if your seeing this contact an Admin!");
    29. }
    30. return;
    31. }
    32.  
    33. }
    34. }
    35. }


    Code:
    2012-09-10 23:32:28 [SEVERE] Could not pass event PlayerDeathEvent to Stronghold2
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:332)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:322)
    at net.minecraft.server.EntityPlayer.die(EntityPlayer.java:307)
    at net.minecraft.server.EntityLiving.damageEntity(EntityLiving.java:663)
    at net.minecraft.server.EntityHuman.damageEntity(EntityHuman.java:595)
    at net.minecraft.server.EntityPlayer.damageEntity(EntityPlayer.java:349)
    at net.minecraft.server.EntityHuman.attack(EntityHuman.java:762)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1020)
    at net.minecraft.server.Packet7UseEntity.handle(SourceFile:36)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
    at com.arnie.sh2.Listeners.PlayerDeath.DeathCharge(PlayerDeath.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
    ... 20 more
     
  2. Offline

    Assult

  3. Offline

    andf54

    Here is a hint:
    Caused by: java.lang.NullPointerException
    at com.arnie.sh2.Listeners.PlayerDeath.DeathCharge(PlayerDeath.java:24)
     
  4. Offline

    arnie231

  5. Offline

    CorrieKay

    Main.economy is null. you should actually read the stack trace next time. 9 times out of 10 whenever i have a problem i post here, it doesnt involve a stacktrace.
     
  6. Offline

    andf54

    You will have to learn reading stacktraces. Those nulls pop out too often. Even for a experienced programmers.
    I even gave you a hint... that points directly to the line where the exception originated. What else do you need?

    Stacktraces are relatively easy to read and usually give all the required information. If you still don’t find what the problem is then use System.out.println() to check if the the different elements from the given line are null.

    That post was made for a reason.

    The only thing worse than "halp, i got a nullpointerexception!!!!" is "halp, it doesnt compile!!!"
     
    CorrieKay likes this.
  7. Offline

    arnie231

    Fixed the issue

    After Player player = event.getEntity();

    i need to add

    Player player = event.getEntity().getPlayer();
     
Thread Status:
Not open for further replies.

Share This Page