NPE with config

Discussion in 'Spigot Plugin Development' started by HippoPlayz, Jun 1, 2018.

  1. Hello there,

    I have been trying to figure out why I keep on getting the NPE's I am, and I have finally given up and I have come here to see if anyone can see what I am not. The code is below. The NPE's I get are on line 39 and 77.

    Thanks in advance for the help!

    Code: https://hastebin.com/uxuzumilik.java

    Line 39:
    Code (Text):
     List<String> rawData = getConfig().getStringList("tokens");
    Line 77:
    Code (Text):
     getConfig().set("tokens", rawData);
     
    #1 HippoPlayz, Jun 1, 2018
    Last edited: Jun 1, 2018
  2. Im not sure but to me it looks like you have declared this twice
    Code (Text):

    [19]   private TokenManager manager;

      [37]      manager = new TokenManager(this);

    Not sure if this could be the issue,
     
  3. what lines are 39 and 77? Also, remove:

    cfile = new File(getDataFolder(), "config.yml");
    if (!cfile.exists()) {
    saveDefaultConfig();
    }
    and just set it to
    saveDefaultConfig();
     
  4. Code (Java):


    package me.bradley.tokens;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.io.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    /**
    * Created by Bradley on 1/6/2018.
    */

    public class Tokens extends JavaPlugin implements Listener {
        private TokenManager manager;
        public FileConfiguration config;
        public File cfile;
        public HashMap<ItemStack, Integer> shopItems = new HashMap<ItemStack, Integer>();
        public HashMap<UUID, Integer> playerTokens = new HashMap<UUID, Integer>();

        @Override
        public void reloadConfig() {
            this.config = YamlConfiguration.loadConfiguration(cfile);
        }

        public void onEnable() {

            cfile = new File(getDataFolder(), "config.yml");
            if (!cfile.exists()) {
                saveDefaultConfig();
            }

            manager = new TokenManager(this);

            List<String> rawData = getConfig().getStringList("tokens");
            for (String raw : rawData) {
                String[] entries = raw.split(":");
                UUID uuid = UUID.fromString(entries[0]);
                int tokens = Integer.parseInt(entries[1]);
                playerTokens.put(uuid, tokens);
            }
            rawData.clear();
            getConfig().set("tokens", null);

            for (int x = 0; x >= 0; x++) {
                if (getConfig().get("items." + x) != null) {
                    ItemStack item = getConfig().getItemStack("items." + x + ".item");
                    int tokenCost = getConfig().getInt("items." + x + ".cost");
                    shopItems.put(item, tokenCost);
                }
            }

            reloadConfig();

            getCommand("token").setExecutor(new TokenCommand(manager));
            getServer().getPluginManager().registerEvents(new TokenListener(manager), this);

        }

        public void onDisable() {

            int x = 0;
            for (ItemStack stack : shopItems.keySet()) {
                getConfig().set("items." + x + ".item", stack);
                getConfig().set("items." + x + ".cost", shopItems.get(stack));
            }

            List<String> rawData = new ArrayList<String>();
            for (UUID uuid : playerTokens.keySet()) {
                String raw = uuid.toString() + ":" + playerTokens.get(uuid);
                rawData.add(raw);
            }
            getConfig().set("tokens", rawData);
            saveConfig();

            rawData.clear();
            playerTokens.clear();
            shopItems.clear();
        }

    }
     
  5. Error is in this part
    List<String> rawData = getConfig().getStringList("tokens");
     
  6. So change it to make a new ArrayList?
     
  7. 1) the list tokens in the config is misspelled.
    2) the list tokens in the config doesn't exist
    3) you should stick with the already existing config methods so just add saveDefaultConfig() onEnable.

    Check those 3
     
  8. Changed the code like you said:
    Code (Text):
     
    package me.bradley.tokens;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.io.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    /**
    * Created by Bradley on 1/6/2018.
    */
    public class Tokens extends JavaPlugin implements Listener {
        private TokenManager manager;
        public FileConfiguration config;
        public File cfile;
        public HashMap<ItemStack, Integer> shopItems = new HashMap<ItemStack, Integer>();
        public HashMap<UUID, Integer> playerTokens = new HashMap<UUID, Integer>();

        @Override
        public void reloadConfig() {
            this.config = YamlConfiguration.loadConfiguration(cfile);
        }

        public void onEnable() {

            saveDefaultConfig();

            manager = new TokenManager(this);

            List<String> rawData = new ArrayList<String>();
            for (String raw : rawData) {
                String[] entries = raw.split(":");
                UUID uuid = UUID.fromString(entries[0]);
                int tokens = Integer.parseInt(entries[1]);
                playerTokens.put(uuid, tokens);
            }
            rawData.clear();
            getConfig().set("tokens", null);

            for (int x = 0; x >= 0; x++) {
                if (getConfig().get("items." + x) != null) {
                    ItemStack item = getConfig().getItemStack("items." + x + ".item");
                    int tokenCost = getConfig().getInt("items." + x + ".cost");
                    shopItems.put(item, tokenCost);
                }
            }

            reloadConfig();

            getCommand("token").setExecutor(new TokenCommand(manager));
            getServer().getPluginManager().registerEvents(new TokenListener(manager), this);

        }

        public void onDisable() {

            int x = 0;
            for (ItemStack stack : shopItems.keySet()) {
                getConfig().set("items." + x + ".item", stack);
                getConfig().set("items." + x + ".cost", shopItems.get(stack));
            }

            List<String> rawData = new ArrayList<String>();
            for (UUID uuid : playerTokens.keySet()) {
                String raw = uuid.toString() + ":" + playerTokens.get(uuid);
                rawData.add(raw);
            }
            getConfig().set("tokens", rawData);
            saveConfig();

            rawData.clear();
            playerTokens.clear();
            shopItems.clear();
        }

    }
    Now I get this error:
    Code (Text):
     
    Error occurred while enabling Tokens v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: File cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[server.jar:git-TacoSpigot-"1be69cbf"]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:175) ~[server.jar:git-TacoSpigot-"1be69cbf"]
            at me.bradley.tokens.Tokens.reloadConfig(Tokens.java:27) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:162) ~[server.jar:git-TacoSpigot-"1be69cbf"]
            at me.bradley.tokens.Tokens.onEnable(Tokens.java:44) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[server.jar:git-TacoSpigot-"1be69cbf"]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [server.jar:git-TacoSpigot-"1be69cbf"]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404) [server.jar:git-TacoSpigot-"1be69cbf"]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:359) [server.jar:git-TacoSpigot-"1be69cbf"]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:318) [server.jar:git-TacoSpigot-"1be69cbf"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:408) [server.jar:git-TacoSpigot-"1be69cbf"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:372) [server.jar:git-TacoSpigot-"1be69cbf"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:327) [server.jar:git-TacoSpigot-"1be69cbf"]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:267) [server.jar:git-TacoSpigot-"1be69cbf"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:563) [server.jar:git-TacoSpigot-"1be69cbf"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    I believe that the saveDefaultConfig() part should have stayed how I had it, but, I will await your response to clarify.
     
  9. remove:
    Code (Java):

        public FileConfiguration config;
        public File cfile;
      @Override
        public void reloadConfig() {
            this.config = YamlConfiguration.loadConfiguration(cfile);
        }
     
    and you should be good to go
     
  10. My server now just freezes and crashes when it tries to enable the plugin. There is no error relating to the plugin but each time, it says "Enabling Tokens v1.0" and then the server freezes and crashes.
     
  11. A plugin loading after tokens v1.0 has to do with the crash. Or you are doing something that is causing the server to crash like a while(true) {} loop or smth.