NullPointer is confusing me

Discussion in 'Spigot Plugin Development' started by HarleySwtfus, Jan 7, 2020.

  1. I have an event that gathers an int value from the config when a block is broken, but it cant seem to find it due to a NullPointer Exception.

    Main class;

    Code (Java):


        static SettingsManager config = new SettingsManager().getInstance();
     
        public void onEnable() {
            System.out.println("[Prison] Plugin Enabled");
            initSetup();
            config.saveConfig();
            config.saveData();
        }
     
        public void onDisable() {
         
        }

        public void initSetup() {
            config.setup(this);
            Bukkit.getServer().getPluginManager().registerEvents( new EventsBreak(), (Plugin) this);
        }
     
        public static void setupPlayer(Player p) {
            if (!(config.getData().isConfigurationSection(p.getName()))) {
                config.setLevel(p, 1);
                config.setXP(p, 0);
                config.saveData();
            } else {
                Bukkit.broadcastMessage("fail");
            }
        }
       
    BreakBlock event:
    Code (Java):

        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            Material m = e.getBlock().getType();
            p.sendMessage(m.toString().toLowerCase());
            sm.giveXPForBlock(p, m);
            p.sendMessage(m.toString().toLowerCase() + "2");
            e.getBlock().setType(Material.STONE);
        }
    "giveXPForBlock method":
    Code (Java):

        public void giveXPForBlock(Player p, Material m) {
            addXP(p, config.getInt("coal_ore"));    <----- line printed on stacktrace
            p.sendMessage("New XP is: " + getXP(p));
        }
    Why is this leading to a nullpointer?
    Config is legit:
    coal_ore: 3
     
  2. config is null
     
  3. I thought I setup config in the initSetup method in main class?
     
  4. Show the #addXP method
     
  5. public void addXP(Player p, int amount) {
    data.set(getUsernameFromUUID(p) + ".xp", getXP(p) + amount);
    }
     
  6. Like... when he said that... he meant everything until the only code left is from any standard-library or primitives. What is data, what is getUsernameFromUUID, what is getXP()?
     
  7. Ahh my bad.

    Code (Java):

        public String getUsernameFromUUID(Player p) {
            return Bukkit.getPlayer(p.getUniqueId()).getName();
        }
    Code (Java):

        public int getXP(Player p) {
            return data.getInt(getUsernameFromUUID(p) + ".xp");
        }
    Code (Java):


        Plugin p;

        FileConfiguration config;

        File cfile;

        FileConfiguration data;

        File dfile;

        public void setup(Plugin p) {
            this.cfile = new File(p.getDataFolder(), "config.yml");
            this.config = p.getConfig();
            this.config.options().copyDefaults(true);
            saveConfig();
            if (!p.getDataFolder().exists())
                p.getDataFolder().mkdir();
            this.dfile = new File(p.getDataFolder(), "data.yml");
            if (!this.dfile.exists())
                try {
                    this.dfile.createNewFile();
                } catch (IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
                }
            this.data = (FileConfiguration) YamlConfiguration.loadConfiguration(this.dfile);
        }

        public FileConfiguration getData() {
            return this.data;
        }

        public void saveData() {
            try {
                this.data.save(this.dfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
            }
        }

        public void reloadData() {
            this.data = (FileConfiguration) YamlConfiguration.loadConfiguration(this.dfile);
        }

        public FileConfiguration getConfig() {
            return this.config;
        }

        public void saveConfig() {
            try {
                this.config.save(this.cfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
            }
        }
     
  8. So you say the config loads correctly and you can access items. Is the same true for the data-config? Did you try checking if that is null?
     
  9. Could you post the stacktrace?
     
  10. First of all replace:
    Code (Java):
    new SettingsManager().getInstance();
    with
    Code (Java):
    SettingsManager.getInstance();
    EDIT: If that method is static that is.
     
  11. Also paste the stack trace entirely, don't omit any parts of it.