Solved Null pointer exception on all events

Discussion in 'Plugin Development' started by Ducyooo, Oct 5, 2020.

  1. Offline

    Ducyooo

    I cant figure out why its giving me a null pointer exception on all my events!
    It is probably the hashmap but I initialize it as soon as the plugin starts so im left with no clue..

    Code:
    Caused by: java.lang.NullPointerException
            at it.ducyo.apocalypse.events.PlayerJoinListener.onJoin(PlayerJoinListener.java:24)

    Main class:
    Code:
    package it.ducyo.apocalypse;
    
    import it.ducyo.apocalypse.events.BlockBreakListener;
    import it.ducyo.apocalypse.events.PlayerJoinListener;
    import it.ducyo.apocalypse.events.PlayerQuitListener;
    import it.ducyo.apocalypse.managers.PlayerLevelManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    public class ApocalypsePlugin extends JavaPlugin {
    
        public HashMap<UUID, PlayerLevelManager> playerManagerHashMap;
    
        @Override
        public void onEnable() {
            this.playerManagerHashMap = new HashMap<>();
            this.getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
            this.getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this);
            this.getServer().getPluginManager().registerEvents(new BlockBreakListener(), this);
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
    
        @Override
        public void onDisable() {
            this.saveConfig();
        }
    }
    Listener class:

    Code:
    package it.ducyo.apocalypse.events;
    
    import it.ducyo.apocalypse.ApocalypsePlugin;
    import it.ducyo.apocalypse.managers.PlayerLevelManager;
    import it.ducyo.apocalypse.utility.Colorize;
    import lombok.RequiredArgsConstructor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    @RequiredArgsConstructor
    public class PlayerJoinListener implements Listener {
    
        public ApocalypsePlugin plugin;
        public PlayerLevelManager playerLevelManager;
    
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player player = (Player) event.getPlayer();
    
            if(!player.hasPlayedBefore()) {
                Colorize.sendMessage(player, "&eWelcome to &6Apocalypse!");
                plugin.playerManagerHashMap.put(player.getUniqueId(), playerLevelManager);
    
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".mining-level", 0);
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".mining-xp", 0);
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".foraging-level", 0);
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".foraging-xp", 0);
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".combat-level", 0);
                plugin.getConfig().set("player-levels." + player.getUniqueId() + ".name"
                        + player.getDisplayName() + ".combat-xp", 0);
            } else {
                Colorize.sendMessage(player, "&eWelcome back to &6Apocalypse!");
                plugin.playerManagerHashMap.put(player.getUniqueId(), playerLevelManager);
            }
        }
    }
    
    EDIT: the error is coming from this line:
    plugin.playerManagerHashMap.put(player.getUniqueId(), playerLevelManager);
    is anything wrong with this??? pls help

    EDIT: got it never initialized a the class i needed..
     
    Last edited: Oct 5, 2020

Share This Page