1.15.2 NullPointerException

Discussion in 'Spigot Plugin Development' started by DvanOosterhout, Feb 3, 2020.

  1. Hello all,

    I am trying to create a small economy plugin for a small server. Everything was going fine until I ran into the famous NullPointerException. I know where it's coming from but I've got no clue on how to fix it.

    The classes I use:
    Code (Java):
    package com.kmx.sjonnieseconomy;

    import java.util.logging.Logger;

    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.kmx.sjonnieseconomy.ConfigManager;

    public class Main extends JavaPlugin {

        private ConfigManager cm;

        Logger log = Logger.getLogger("Minecraft");
        PluginDescriptionFile file = getDescription();
        PluginManager pm = getServer().getPluginManager();
     
        public void onEnable() {
            log.info("[SjonniesEconomy]" + file.getName() + " Version " + file.getVersion() + " has been Enabled!");
         
            configStart();
            registerCommands();
            pm.registerEvents(new Events(), this);
        }
     
        public void onDisable() {
            log.info("[SjonniesEconomy]" + file.getName() + " Version " + file.getVersion() + " has been Disabled!");

            configSave();
        }
     
        public void configStart() {
            cm = new ConfigManager();
            cm.setupBank();
            cm.reloadBank();
        }
     
        public void configSave() {
            cm.saveBank();
        }
     
        public void registerCommands() {
            this.getCommand("bal").setExecutor(new Commands());
            this.getCommand("balance").setExecutor(new Commands());
        }
    }
     

    Code (Java):
    package com.kmx.sjonnieseconomy;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import com.kmx.sjonnieseconomy.Main;

    public class ConfigManager {

        private Main plugin = Main.getPlugin(Main.class);

        FileConfiguration bankcfg;
        File bankfile;
     
        public void setupBank() {
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
            }

            bankfile = new File(plugin.getDataFolder(), "bank.yml");

            if (!bankfile.exists()) {
                try {
                    bankfile.createNewFile();
                    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SjonniesEconomy] The bank.yml file has been created");
                } catch (IOException e) {
                    Bukkit.getServer().getConsoleSender()
                            .sendMessage(ChatColor.RED + "[SjonniesEconomy] Could not create the bank.yml file");
                }
            }

            bankcfg = YamlConfiguration.loadConfiguration(bankfile);
        }

        public FileConfiguration getBank() {
            return bankcfg;
        }

        public void saveBank() {
            try {
                bankcfg.save(bankfile);
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "[SjonniesEconomy] The bank.yml file has been saved");

            } catch (IOException e) {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "[SjonniesEconomy] Could not save the bank.yml file");
            }
        }

        public void reloadBank() {
            bankcfg = YamlConfiguration.loadConfiguration(bankfile);
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[SjonniesEconomy] The bank.yml file has been reload");

        }
     
        public boolean playerExists(Player player) {
            if(bankcfg.contains(player.getUniqueId().toString())) {
                return false;
            }else{
                return true;
            }
        }

        public void newPlayer(Player player, int amount) {
            if(playerExists(player)) {return;}
         
            String uuid = player.getUniqueId().toString();
         
            bankcfg.set(uuid + ".Name", player.getName());
            bankcfg.set(uuid + ".Money", amount);
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[SjonniesEconomy] User profile created for " + player.getName() + "!");
         
            saveBank();
        }
     
        public int getBalance(Player player) {
            String uuid = player.getUniqueId().toString();
            int balance = bankcfg.getInt(uuid + ".Money");
            return balance;
        }
     
        public void bankToPlayer(Player player, int amount) {
         
        }
     
        public void playerToPlayer(Player sender, int amount, Player receiver) {
         
        }
     
        public void playerToBank(Player player, int amount) {
         
        }
    }

    Code (Java):
    package com.kmx.sjonnieseconomy;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import net.md_5.bungee.api.ChatColor;

    public class Events implements Listener {
     
        //------------- Variables
        private ConfigManager cm = new ConfigManager();
        //-------------
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            cm.newPlayer(player, 10000);
            player.sendMessage(ChatColor.YELLOW + "Your current balance is " + ChatColor.GREEN + cm.getBalance(player));
        }
    }
     

    [Server thread/ERROR]: Could not pass event PlayerJoinEvent to SjonniesEconomy v0.1
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.PlayerList.a(PlayerList.java:176) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.LoginListener.c(LoginListener.java:154) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.LoginListener.tick(LoginListener.java:53) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.NetworkManager.a(NetworkManager.java:220) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.ServerConnection.c(ServerConnection.java:129) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.MinecraftServer.b(MinecraftServer.java:1105) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:399) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:984) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:824) [spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
    at com.kmx.sjonnieseconomy.ConfigManager.playerExists(ConfigManager.java:62) ~[?:?]
    at com.kmx.sjonnieseconomy.ConfigManager.newPlayer(ConfigManager.java:70) ~[?:?]
    at com.kmx.sjonnieseconomy.Events.onPlayerJoin(Events.java:19) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.15.2.jar:git-Spigot-800b93f-8160e29]
    ... 13 more

    I know the error comes from calling the file within the class 'ConfigManager' named 'bankfile' or 'bankcfg'. I've tried everything I know for the past hour but nothing worked. It's probably something simple but I can't find it.

    P.S. I don't know if this classifies as Java or Spigot so I thought I'd shout my shot. Also English is not my first language so please excuse any spelling and/or grammatical mistakes.

    Thank you in advance!
     
  2. SteelPhoenix

    Moderator

    Your bank config is never loaded in your events class. Also, be careful; it's likely you will get concurrency issues if you have different config managers access the same file.
     
  3. First of all, thank you for the quick reply!

    Second, how do i do that? I tried loading it the same way I did in the ConfigManager File. But never succeeded.

    Code (Java):

        FileConfiguration bankcfg;
        File bankfile;
        private Main plugin = Main.getPlugin(Main.class);
     
        public void loadConfig() {
            bankfile = new File(plugin.getDataFolder(), "bank.yml");
            bankcfg = YamlConfiguration.loadConfiguration(bankfile);
        }

    Then loaded it in the onPlayerJoin Event
    Code (Java):
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            loadConfig();
            Player player = e.getPlayer();
            cm.newPlayer(player, 10000);
            player.sendMessage(ChatColor.YELLOW + "Your current balance is " + ChatColor.GREEN + cm.getBalance(player));
        }
     
  4. SteelPhoenix

    Moderator

    cm.newPlayer(player, 10000);
    You never loaded the configuration in your ConfigManager in your events class.
     
  5. I'm sorry but I still don't understand. How do load my configuration from ConfigManager to Events? I tried adding the cm.getBank() to the event class and getBank() to all methods in ConfigManager, still no luck.
     
  6. in your onEnable, you load the bank and stuff like you should. that is for the instance of the object in your main class. however, you create a whole new instance for the config stuff in your events class, but you do not call these methods. so the instance of the config stuff in your events class has stuff that isnt loaded, while the one in the onEnable does. the "concurrency issues" mentioned before is if these two different instances are modified at different times and then saved, whichever is saved first will be overwritten. you need to use one instance and not create a whole new instance of configmanager.