NPE with getDataFolder

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 16, 2016.

  1. This code is run when your class is loaded but has not yet been enabled and therefore your onEnable has yet to be called.
    Code (Text):
    private Main plugin;
        Manager manager = new Manager(plugin);
        FileConfiguration config = manager.getConfig();
     
    This means that "plugin" is null and therefor the plugin passed into the Manager constructor is also null. Then when you call
    Code (Text):
    FileConfiguration config = manager.getConfig();
    you call the method that calls plugin.getDataFolder() which is throwing a null pointer.

    You should wait to assign the values to the "manager" and "config" fields until your onEnable is called. So move the initialization into the onEnable.
     
  2. File whatever = new File(getDataFolder(), "whatever.yml");
    FileConfiguration whateverGetter = YAMLConfiguration.loadConfiguration(whatever);

    Custom YML file^
     
  3. Did you even read my post?


    And

    Ok so like this?
    Code (Java):

    package Starter;

    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
        private Main plugin;
        public void onEnable(){
            new Manager(this);
            Manager manager = new Manager(plugin);
            manager.exists();
        }
        public void registerEvents(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new StartCount(this), this);
        }
    }
     
    I did that and just set
    Code (Java):
    file = new File(plugin.getDataFolder(), FileName);
    to the entire manager and now I'm getting the error at
    Code (Java):

    public boolean exists(){
            if(!file.exists()){                                     //Here is the error
                if (!plugin.getDataFolder().exists()) {
                    plugin.getDataFolder().mkdirs();
                }
                plugin.saveResource(FileName, false);
            }
            return true;
        }
     
    Error
    Code (Text):

    java.lang.NullPointerException
            at Starter.Manager.<init>(Manager.java:12) ~[?:?]
            at Starter.Main.onEnable(Main.java:9) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin(CraftServer.java:361) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins(CraftServer.java:321) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.reload(CraftServer.java:745) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.Bukkit.reload(Bukkit.java:539) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:631) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:438) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:401) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
     
     
  4. Yes, I did. I gave you a working fix that cuts out all the bullshit you keep attempting.
     
  5. Ok in order for you to keep your sanity you are going to have to learn how to debug these things on your own because there is no way you are going to get anything done if you have to wait for a reply on here for small errors like this one that aren't too challenging to track down.

    Something on that line is null. There is only one thing on that line, the variable "file" and so you need to figure out why, at the time of calling the exists() method, "file" is null.
     
    • Agree Agree x 1
  6. Don't help him, he's one of those YouTube taught, and steals code from Bukkit forums/threads types. Although, that isn't a reason to not help, but he's also attacking me when I gave him a fix.
     
  7. I have never stolen code nor do i care for those tutorial youtube videos.

    The only reason i ask on the forums is because I couldn't find a solution
     
  8. And to you you didn't make a method to save, no method to call it and nothing to be sure that it exists, so how can those two lines be superior?
     
  9. whateverGetter.save(whatever);

    Happy?
     
  10. and if it doesn't exist when you try it?
     
  11. if (whatever.exists()) {
    whateverGetter.save(whatever);
    } else {
    whatever.createNewFile();
    }
     
  12. So noone is going to give any useful information?