NPE when getting a value from the config with a method.

Discussion in 'Plugin Development' started by ESSHD, Apr 25, 2015.

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

    ESSHD

    Hi, I'm making a kits plugin for my server and I need to check if the player's balance is null, and if it is, setup their account.

    Listener class: (Where the NPE is located)
    Code:
    package com.shockmc.listeners;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import com.shockmc.Kitpvp;
    import com.shockmc.MessageManager;
    
    public class FirstJoin implements Listener {
       
        public static Kitpvp main;
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = (Player) e.getPlayer();
            if (main.getTokens().getTokens(p).toString() == null) {
                MessageManager.sendGood(p, "Welcome to MadKits, " + p.getName() + ". We've setup your account for you.");
                main.getTokens().setUp(p, 5);
                return;
            }
            MessageManager.sendGood(p, "Welcome back, " + p.getName() + ". Your token balance is " + main.getTokens().getTokens(p));
        }
    
    }
    Tokens class:
    Code:
    package com.shockmc.tokens;
    
    import org.bukkit.entity.Player;
    
    import com.shockmc.Kitpvp;
    import com.shockmc.MessageManager;
    
    public class Tokens {
       
        static Kitpvp main;
       
        public Integer getTokens(Player p) {
            Integer tokens = main.getConfig().getInt(p.getUniqueId() + ".balance");
            if (tokens == null) {
                return null;
            }
            return tokens;
        }
       
        public void setTokens(Player p, Integer amount) {
            main.getConfig().set(p.getUniqueId() + ".balance", amount);
            MessageManager.sendWarning(p, "Tokens set to " + amount);
            main.saveConfig();
        }
       
        public void addTokens(Player p, Integer amount) {
            Integer current = main.getConfig().getInt(p.getUniqueId() + ".balance");
            main.getConfig().set(p.getUniqueId() + ".balance", current + amount);
            MessageManager.sendGood(p, "+" + amount + " tokens");
            main.saveConfig();
        }
       
        public void removeTokens(Player p, Integer amount) {
            Integer current = main.getConfig().getInt(p.getUniqueId() + ".balance");
            main.getConfig().set(p.getUniqueId() + ".balance", current - amount);
            MessageManager.sendBad(p, "-" + amount + " tokens");
            main.saveConfig();
        }
       
        public void createTransaction(Player p1, Player p2, Integer removeAmount, Integer addAmount) {
            Integer p1bal = main.getConfig().getInt(p1.getUniqueId() + ".balance");
            Integer p2bal = main.getConfig().getInt(p2.getUniqueId() + ".balance");
            main.getConfig().set(p1.getUniqueId() + ".balance", p1bal - removeAmount);
            main.getConfig().set(p2.getUniqueId() + ".balance", p2bal + addAmount);
            MessageManager.sendBad(p1, "-" + removeAmount + " tokens");
            MessageManager.sendGood(p2, "+" + addAmount + " tokens");
            main.saveConfig();
        }
       
        public void setUp(Player p, int amount) {
            main.getConfig().set(p.getUniqueId() + ".balance", 5);
            main.getConfig().set(p.getUniqueId() + ".username", p.getName());
            main.saveConfig();
        }
    
    }
    Console error:
    Code:
    [16:42:45 ERROR]: Could not pass event PlayerJoinEvent to Kits v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.PlayerList.onPlayerJoin(PlayerList.java:
    296) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.PlayerList.a(PlayerList.java:156) [custo
    m.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.LoginListener.b(LoginListener.java:144)
    [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.LoginListener.c(LoginListener.java:54) [
    custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.NetworkManager.a(NetworkManager.java:231
    ) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.ServerConnection.c(ServerConnection.java
    :148) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:8
    09) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [custom.jar:git-Spigot-dbe012b-61ef214]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.NullPointerException
            at com.shockmc.listeners.FirstJoin.onPlayerJoin(FirstJoin.java:18) ~[?:?
    ]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_31]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            ... 14 more
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    mythbusterma

    @ESSHD

    Main is null in both those contexts....also it shouldn't be static.
     
  3. Offline

    ESSHD

  4. Offline

    mythbusterma

    @ESSHD

    Initilise 'main' to a value, and remove the static operator.
     
  5. Offline

    ESSHD

    @mythbusterma I think it's fixed, I'll have to check. Thanks.

    @mythbusterma Now it's springing an error.
    Code:
    [18:33:37 INFO]: [Kits] Enabling Kits v1.0
    [18:33:37 ERROR]: Error occurred while enabling Kits v1.0 (Is it up to date?)
    java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader
    .java:122) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[custom
    .jar:git-Spigot-dbe012b-61ef214]
            at com.shockmc.Kitpvp.<init>(Kitpvp.java:16) ~[?:?]
            at com.shockmc.listeners.FirstJoin.<init>(FirstJoin.java:13) ~[?:?]
            at com.shockmc.Kitpvp.onEnable(Kitpvp.java:23) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[c
    ustom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:335) [custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugin(CraftServer.jav
    a:356) [custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.enablePlugins(CraftServer.
    java:316) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.r(MinecraftServer.java:4
    16) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.k(MinecraftServer.java:3
    82) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.a(MinecraftServer.java:3
    37) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.jav
    a:257) [custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :522) [custom.jar:git-Spigot-dbe012b-61ef214]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader
    .java:125) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[custom
    .jar:git-Spigot-dbe012b-61ef214]
            at com.shockmc.Kitpvp.<init>(Kitpvp.java:16) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ~[?:1.8.0_31]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    ~[?:1.8.0_31]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.8.0_31]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_3
    1]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:76) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:131) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:329) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:251) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.loadPlugins(CraftServer.ja
    va:291) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            at net.minecraft.server.v1_8_R2.DedicatedServer.init(DedicatedServer.jav
    a:199) ~[custom.jar:git-Spigot-dbe012b-61ef214]
            ... 2 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  6. Offline

    mythbusterma

    @ESSHD

    I didn't say 'make a new instance of your main class,' I said to initialise the reference to it.
     
    Konato_K likes this.
  7. Offline

    ESSHD

    @mythbusterma Could you show me how? I don't get what you're trying to do.
     
  8. Offline

    1Rogue

Thread Status:
Not open for further replies.

Share This Page