Null Pointer on a primitive type.

Discussion in 'Plugin Development' started by TheGamesHawk2001, Apr 4, 2015.

Thread Status:
Not open for further replies.
  1. Hey all, I'm trying to get a boolean from a config but it keeps giving me a NPE when I clearly gave it the correct path..


    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onBlockBreak(BlockBreakEvent event) {
    Player p = event.getPlayer();
    UUID pid = p.getUniqueId();

    if (Main.config.getInt(pid.toString() + ".options" + ".total-mined") > 2500) {
    if (Main.config.getInt(pid.toString() + ".options" + ".total-mined") % 2500 == 0) {
    Main.config.set(pid.toString() + ".tokens", Main.config.getInt(pid.toString() + ".tokens", 0) + 1);
    plugin.saveConfig();
     
  2. Offline

    WeeSkilz

    Your config could be null.
     
  3. Yes it is. Hence the NPE... NULL pointer exception. I'm saying why. I have set up the join listener:

    Join Listener:
    Code:
        @EventHandler (priority = EventPriority.HIGHEST)
        public void playerJoin(PlayerJoinEvent event) {
            Player p = (Player) event.getPlayer();
            UUID pid = p.getUniqueId();
    
    
            ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
    
            ItemMeta pickaxeMeta = pickaxe.getItemMeta();
            pickaxeMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Diamond Pickaxe");
            pickaxeMeta.addEnchant(Enchantment.DIG_SPEED, 9, true);
    
            pickaxe.setItemMeta(pickaxeMeta);
            if(!getConfig().contains(pid.toString() + ".tokens")) {
                getConfig().set(pid.toString() + ".options" + ".name", p.getName());
                getConfig().set(pid.toString() + ".options" + ".is-in-booster-timer", false);
                getConfig().set(pid.toString() + ".options" + ".total-mined", 0);
                getConfig().set(pid.toString() + ".options" + ".pick-claimed", false);
                getConfig().set(pid.toString() + ".tokens", 0);
                getConfig().set(pid.toString() + ".experience", 1);
                getConfig().set(pid.toString() + ".total", 1);
                getConfig().set(pid.toString() + ".nvlvl", 0);
                getConfig().set(pid.toString() + ".hastelvl", 0);
                getConfig().set(pid.toString() + ".exlvl", 0);
                p.getInventory().addItem(pickaxe);
                p.updateInventory();
                saveConfig();
            }
        }
     
  4. Offline

    mine-care

    @TheGamesHawk2001 umm don't abuse static. It is the cause of some problems , prefer loading gonfig data onEnable instead of constant saving & loading...
     
  5. I need to update something o nthe pickaxe so I have to.
     
  6. Offline

    WeeSkilz

    @TheGamesHawk2001 you are making your config static. Make a #getInstance() method in your Main and have it return an instance of your plugin. Then you can do
    Code:
    Configuration config = Main.getInstance().getConfig();
     
  7. Invisible

    nverdier

    Or just pass the instance through a constructor.
     
    mine-care and teej107 like this.
  8. already am :)
     
Thread Status:
Not open for further replies.

Share This Page