NPE when getting value from config

Discussion in 'Plugin Development' started by bobthefish, Feb 22, 2015.

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

    bobthefish

    Im trying to get a value from the config in a separate class from main, but it keeps coming up with a null pointer exception. Here is my onEnable, the separate class, and the stacktrace:


    Code:
       public void onEnable() {
        setupConfig();
        shopmeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Cosmetics");
        shop.setItemMeta(shopmeta);
        prefsmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Settings");
        prefs.setItemMeta(prefsmeta);
        plugin = this;
        ListenerJoin lj = new ListenerJoin(this);
        sim = new ShopInventoryManager(this);
        pim = new PrefsInventoryManager(this);
        for (Player p : Bukkit.getOnlinePlayers()) {
            lj.checkPlayersStuff(p);
        }
        new Listeners(this);
        new JumpingManager(this);
        ssm = new ServerSelectionManager(this);
        sql = new SQL(this);
        sql.openConnection();    

    Code:
    public class SQL {
        MainHub pl;
    
        public SQL(MainHub plugin) {
        pl = plugin;
        }
    
        private String hostIP = "jdbc:mysql://" + pl.
            getConfig().
            getString("host");
        private String port = pl.getConfig().getString("port");
        private String name = pl.getConfig().getString("name");
        private String uuidField = "uuid";
        private String tableName = "ml_shop";
    Code:
    [01:55:00] [Server thread/ERROR]: Error occurred while enabling Hub version Beta 1.0 (Is it up to date?)
    java.lang.NullPointerException
        at me.BobTheFish.Hub.SQL.<init>(SQL.java:19) ~[?:?]
        at me.BobTheFish.Hub.MainHub.onEnable(MainHub.java:53) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.java:476) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.java:394) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.reload(CraftServer.java:866) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.Bukkit.reload(Bukkit.java:301) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [server.jar:git-Spigot-1.7.9-R0.2-204-g534549b]

    EDIT: Line 53 of MainHub is sql = new SQL(this); and line 19 in SQL is getConfig().
     
  2. Offline

    mythbusterma

    @bobthefish

    Because you initialise the values from SQL in the clinit, instead of in the constructor, where the value of plugin isn't null.
     
    tomudding likes this.
  3. Offline

    bobthefish

    Im not sure I understand completely
    @mythbusterma, but do you mean I need to put the actual values in the constructor for the class?

    something like

    Code:
    String host;
    
    public SQL(MainHub pl){
    
    host = ....;
    }
    ?
     
  4. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page