Solved Creating a config - NPE

Discussion in 'Plugin Development' started by ReeseNator, Dec 16, 2015.

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

    ReeseNator

    Hi everyone, I am trying to create a config. I've followed both the actual Bukkit documentation, and, examples via the resources sub-section. Unfortunately, I continuously get an NPE. Please point out what I am doing wrong, and hopefully you can help me fix this.

    Here is what is in my onEnable:

    Code:
     public void onEnable() {
            getServer().getPluginManager().registerEvents(new Listener(), this);
            if (!new File(getDataFolder(), "config.yml").exists()) {
                saveDefaultConfig();
            }
            saveConfig();
    }
    
    Here is my Listener class:

    Code:
    package net.reesenator.tokez.Utils;
    
    import net.reesenator.tokez.Main;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class Listener implements org.bukkit.event.Listener {
        private Main plugin;
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            plugin.getConfig().set("name", "ReeseNator");
            String myName = plugin.getConfig().getString("name");
            p.sendMessage(myName);
            plugin.saveConfig();
        }
    }
    
    Here is what I set in the config:

    Code:
    name: ReeseNator
    
    Here is the stacktrace:

    Code:
    [20:20:59 ERROR]: Could not pass event PlayerJoinEvent to Tokez v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:814) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException
            at net.reesenator.Tokez.Utils.Listener.onPlayerJoin(Listener.java:14) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 14 more
    
    Please help me fix it!

    ~ Reese
     
  2. Offline

    Zombie_Striker

  3. Offline

    ReeseNator

  4. Offline

    teej107

    @ReeseNator NPEs are an easy fix. Maybe it'll help if you looked up what causes NPEs
     
  5. Offline

    ReeseNator

    I feel like you guys are being just plain rude. I know what an NPE is. But if it'd help for you to actually look at my code, rather than just reading the title and giving the response you did, you could actually help me locate the issue. As you can see, the config isn't missing, and/or the key isn't either. I've look at that already, and I'm here because I'm stuck. Unfortunate that I have to explain that.

    Regards.
     
  6. Offline

    teej107

    @ReeseNator I did look at your code. Something else is "missing". Now what else do you see that you could be using and may also be null?
     
  7. Offline

    ReeseNator

    The only thing from looking at my code that could potentially be null is the string. But I don't see why it would be.
     
  8. Offline

    teej107

    Tell me all of the objects you are using in the line that is throwing the error.
     
  9. Offline

    ReeseNator

    Ok, from searching for hours now, it seems it's null because I didn't add any code to find the config file. I could be wrong, but that's what it appears to be.
     
  10. Offline

    teej107

     
  11. Offline

    ReeseNator

    Name and ReeseNator.
     
  12. Offline

    teej107

    Those are the String objects. You are using another Object on that line.
     
  13. Offline

    ReeseNator

    @teej107 would the third be plugin?
     
  14. Offline

    teej107

    It is a reference to an Object isn't it? Try debugging by printing out to the console whether objects are null or not.
     
  15. Offline

    mcdorli

    teej, it's not neccessary, please pinpoint the line where it could instantiate the plugin variable.
    Code:
    public class Listener implements org.bukkit.event.Listener {
       private Main plugin;
    
        @EventHandler
       public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            plugin.getConfig().set("name", "ReeseNator");
           String myName = plugin.getConfig().getString("name");
            p.sendMessage(myName);
            plugin.saveConfig();
       }
    }
    
    @ReeseNator
    1.: You don't own the net.reesenator domain. Use me.reesenator or create a github page.
    2.: You don't need to save the config in the onEnable()
     
  16. Offline

    ReeseNator

    I didn't say I own the domain? I just used it for my package, as most people do.
     
  17. Offline

    mcdorli

    You can't. It's not your domain. Most people use domains, brcause they have them.
     
  18. Offline

    ReeseNator

    Pretty sure you can, what can happen if I use it?
     
  19. Offline

    mcdorli

    Your bukkit plugins can get removed, if someone buys that domain
     
  20. Offline

    ReeseNator

    Ah, well I won't be posting it on Bukkit anyways.
     
  21. Offline

    Zombie_Striker

    That's not really a good reason to not do the legal thing.
    This is the broken line.
    • Plugin can be null
    • The config cannot be null.
    • The "name" string cannot be null
    • "ReeseNator" cannot be null.
    This is why @teej107 was trying to walk you through this problem. It is extremely simple.
     
  22. Offline

    ReeseNator

    As I said, I'm pretty sure I just need to call the config from the folder, which I didn't do. I'll test it, and let you all know if that fixes it. And, I didn't realize just "net" would be illegal. I've seen it done many times though. But alright.
     
  23. Offline

    mythbusterma

    @ReeseNator

    It's not "illegal" per se, it just violates convention to use a domain you don't own. For example, my packages always begin with com.mythbusterma because I own the domain mythbusterma.com (there's nothing on the website right now, though).
     
    Mrs. bwfctower likes this.
  24. Offline

    ReeseNator

    Ah, alright. I'll change it then.
     
  25. Offline

    Gorbit99

    You wont have problems immediately, but you can get fined if the owner of the domain notices, that you use it. My favorite one sofar is mr @GoogleMaps package, he uses com.googlemaps, and well, googlemaps.com is owned by, you guessed it, google
     
  26. Offline

    ReeseNator

    Ah, although I assume the most suits will be coming from privately owned domains, not large companies such as Google. But then again, anything is possible. :3
     
  27. Offline

    mcdorli

    Back to your real problem, because off-topicing is not really good. You never initialize the plugin variable. Create a constructor with the Main as argument
     
  28. Offline

    ReeseNator

    I get an error when I register the events for the class in my main.
     
  29. Offline

    boomboompower

    I don't think u need all that.

    This is how saveDefaultConfig() works
    Code:
    public void saveDefaultConfig() {
            File customConfigFile = null;
            if (customConfigFile == null) customConfigFile = new File(getDataFolder(), "config.yml");
           if (!customConfigFile.exists()) this.saveResource("config.yml", false);
        }
    so instead of all your checking, just do saveDefaultConfig();

    Your onEnable could just be
    Code:
    public void onEnable() {
            getServer().getPluginManager().registerEvents(new Listener(), this);
            saveDefaultConfig();
    }
    You could also have the listener in the main class.
     
  30. Offline

    ReeseNator

    But then wouldn't I need to do everything in regards to managing the config in the main class?
     
Thread Status:
Not open for further replies.

Share This Page