Custom Health (Error)

Discussion in 'Plugin Development' started by GamerzKing, Nov 24, 2015.

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

    GamerzKing

    Hey guys!

    So currently, I am making a game, and it requires that I code custom health for the abilities and such, but when I attempt to get the health of the player, it throws me an error on line 17, for null pointer exception.


    Code:
    package com.GamerzKing.elementals.health;
    
    import java.util.HashMap;
    
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    
    public class CustomHealth {
    
        public static HashMap<String, Integer> playerHealth = new HashMap<>();
    
        public static void setHealth(Entity player, int health, HashMap<String, Integer> hashmap) {
            hashmap.put(player.getName(), health);
        }
    
        public static int getHealth(Player player, HashMap<String, Integer> hashmap) {
            return hashmap.get(player.getName());
        }
    }

    Code:
    24.11 12:20:30 [Server] ERROR Could not pass event InventoryClickEvent to Elementals v2.0.1
    24.11 12:20:30 [Server] INFO org.bukkit.event.EventException
    24.11 12:20:30 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    24.11 12:20:30 [Server] INFO Caused by: java.lang.NullPointerException
    24.11 12:20:30 [Server] INFO at com.GamerzKing.elementals.health.CustomHealth.getHealth(CustomHealth.java:17) ~[?:?]
    24.11 12:20:30 [Server] INFO at com.GamerzKing.elementals.utils.SpectatorGUI.onClick(SpectatorGUI.java:67) ~[?:?]
    24.11 12:20:30 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_45]
    24.11 12:20:30 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.jar:git-Spigot-d97e08b-5e85956]
    24.11 12:20:30 [Server] INFO ... 15 more
    If anyone could help me with this, that would be greatly appreciated!

    Thanks,
    - GamerzKing
     
  2. Offline

    mcdorli

    1.:Don't abuse statics
    2.: Don't use player names, use uuids
    3.: You don't own the gamerzking.com
    4.: Delete the hashmap from the getHealths argument list, currently, you're getting a hashmap from the main class (I guess) and then searching in it for the player
     
    Zombie_Striker likes this.
  3. Offline

    teej107

    @GamerzKing You do know that there is an actual method in the Bukkit API to set the health for Damageables?
     
  4. Offline

    Zombie_Striker

    topest kek.
    Something is null on line 17. debug to find what is null.
     
  5. Offline

    GamerzKing

    Custom health, I need a custom class for it

    I realized it was because my HashMap doesn't save when the server is being reloaded. It sets your health when you join, and then it works, but when it reloads, it is set to null, and doesn't work, which isn't a problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 24, 2015
Thread Status:
Not open for further replies.

Share This Page