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?


    Ok so like this?
    Code (Java):

    package Starter;

    import org.bukkit.plugin.PluginManager;

    public class Main extends JavaPlugin{
        private Main plugin;
        public void onEnable(){
            new Manager(this);
            Manager manager = new Manager(plugin);
        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.saveResource(FileName, false);
            return true;
    Code (Text):

            at Starter.Manager.<init>( ~[?:?]
            at Starter.Main.onEnable( ~[?:?]
            at ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.loadPlugin( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.enablePlugins( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.reload( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.Bukkit.reload( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.command.defaults.ReloadCommand.execute( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.command.SimpleCommandMap.dispatch( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.DedicatedServer.aL( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C( [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f]
            at 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?

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