Solved NullPointerException when returning a integer

Discussion in 'Plugin Development' started by rbrick, Feb 8, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    rbrick

    I am making a economy plugin and after getting the economy data file done i decided to do a /balance command. So i wrote this code in my "api" :

    Show Spoiler

    Code:java
    1.  
    2. public int getCredits(Player p){
    3. int i = settings.getEcoData().getInt("eco.players." + p.getName() + ".credits");
    4. return i;
    5. }


    I have another economy plugin written already and that was the code i used and it worked flawlessly.

    Then i get write the command and in my code i send the player there balance using the method above like so:
    Show Spoiler

    Code:java
    1.  
    2. p.sendMessage(ChatColor.GRAY + "Credits: " + ChatColor.GOLD + api.getCredits(p) + "c");



    And when i execute the command i get a big long error that is basically saying it was caused by a NullPointerException:
    Show Spoiler

    [​IMG]

    Sorry for the picture i couldnt find the latest log file :p
    Here is my OnEnable:
    Show Spoiler

    Code:
        public void onEnable() {
        //Commands
        settings.setup(this);
     
        getCommand("admin").setExecutor(new CmdAdmin(this));
        getCommand("cc").setExecutor(new CmdClearChat());
        getCommand("setspawn").setExecutor(new CmdSetSpawn());
        getCommand("spawn").setExecutor(new CmdSpawn());
        getCommand("setwarp").setExecutor(new CmdSetWarp());
        getCommand("warp").setExecutor(new CmdWarp());
        getCommand("fly").setExecutor(new CmdFly());
        getCommand("delwarp").setExecutor(new CmdDelWarp());
        getCommand("ban").setExecutor(new CmdBan());
        getCommand("unban").setExecutor(new CmdUnban());
        getCommand("kick").setExecutor(new CmdKick());
        getCommand("heal").setExecutor(new CmdHeal());
        getCommand("invsee").setExecutor(new CmdInvSee());
        getCommand("vanish").setExecutor(new CmdVanish());
        getCommand("craft").setExecutor(new CmdCraft());
        getCommand("kill").setExecutor(new CmdKill());
        getCommand("help").setExecutor(new CmdHelp());
        getCommand("gamemode").setExecutor(new CmdGamemode());
        getCommand("who").setExecutor(new CmdWho());
        getCommand("lag").setExecutor(new CmdLag());
        getCommand("mute").setExecutor(new CmdMute(this));
        getCommand("pl").setExecutor(new CmdPlugins(this));
        getCommand("balance").setExecutor(new CmdBal());
        getServer().getPluginManager().registerEvents(new OnJoinListener(), this);
        getServer().getPluginManager().registerEvents(new OnChatListener(this), this);
        getServer().getPluginManager().registerEvents(new OnLoginListener(), this);
        getServer().getPluginManager().registerEvents(new OnRespawn(), this);
        getServer().getPluginManager().registerEvents(new OnServerPingListener(), this);
        saveDefaultConfig();
        setupPermissions();
        setupChat();
        setupEconomy();
        }
     
  2. Offline

    reider45

    What is line 25 in CmdBal?
     
  3. Offline

    PatoTheBest

    Code:java
    1. public int getCredits(Player p){
    2. if (settings.getEcoData().getInt("eco.players." + p.getName() + ".credits") != null){
    3. int i = settings.getEcoData().getInt("eco.players." + p.getName() + ".credits");
    4. return i;
    5. }
    6. }
     
    rbrick likes this.
  4. Offline

    ztowne13

    If it's a null pointer exception, it means that the variable you are trying to get doesn't exist... So make sure that the value actually equals something in the config file or wherever the data is being stored.
     
    rbrick likes this.
  5. Offline

    PatoTheBest

  6. Offline

    ztowne13

    PatoTheBest likes this.
  7. Offline

    rbrick

    i have tried PatoTheBest method(making sure it is not null, which it isnt null) and i still get the error.
     
  8. Offline

    PatoTheBest

    You have essentials, Your plugin is conflicting with essentials. Look at the error, one line before the last line.
     
  9. Offline

    rbrick

    Good guess but this economy is part of my essentials plugin which is what you saw on the last line. i am rewriting it :p
     
  10. Offline

    xTigerRebornx

    rbrick There are pleanty of things that can be null
    Code:
    settings.getEcoData()
    Code:
    api.getCredits(p)
    api could be null, settings could be null, settings.getEcoData() could be returning null. Find out which is null, if the config value isn't
     
    rbrick likes this.
  11. Offline

    rbrick

    Thank you. Instead of doing the method i just did settings.getEcoData() and it worked :D I have tagged it as solved thank you.
     
Thread Status:
Not open for further replies.

Share This Page