Solved File location null

Discussion in 'Spigot Plugin Development' started by BocaRaton, May 17, 2018 at 2:27 PM.

  1. Hello all,

    I haven't worked much with Spigot 1.12 in regard to the resource API and I'm having many issues with loading and saving files. Basically what I'm trying to do is have a folder within my plugin's datafolder to hold more yml files. I have a PanelFile class which loads first the directory and then the files inside. I've tried using absolute/relative paths to get to the file location and nothing has worked. The only thing that has actually worked so far is using the relative string location exactly in the File instantiation "plugins/CarrotMC/panels/panelname.yml'


    Code (Text):
        public void load() {
          /* LINE 25 */  File panelsDir = new File(plugin.getDataFolder() + File.separator + "panels");
            if (!panelsDir.exists()) {
                panelsDir.mkdir();
            }

            for (String panel : panels) {
                if (this.file == null) {
                    this.file = new File(panelsDir.getPath(), panel + ".yml");
                    if (this.file.exists()) {
                        this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
                    } else if (this.plugin.getResource("panels/" + panel + ".yml") != null) {
                        this.plugin.saveResource("panels/" + panel + ".yml", true);
                        this.file = new File(this.plugin.getDataFolder() + File.separator + "panels", panel + ".yml");
                        this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file);
                    } else {
                        this.plugin.getLogger().severe("Could not find panels file! " + panel);
                    }
                }

            }
        }
    Stack-Trace
    Code (Text):
    [09:27:01 INFO]: [CarrotMC] Enabling CarrotMC v0.1
    [09:27:01 ERROR]: Error occurred while enabling CarrotMC v0.1 (Is it up to date?)
    java.lang.NullPointerException: null
        at com.bocaraton.carrotmc.configuration.PanelFile.load(PanelFile.java:25) ~[?:?]
        at com.bocaraton.carrotmc.Core.onEnable(Core.java:42) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:374) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:323) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:421) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:382) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:337) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:544) [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
     
     
  2. Benz56

    Supporter

    Have you checked if plugin == null?
     
  3. We need your full class to help you. As @Benz56 said, your plugin variable is probably null.
     
  4. Your plugin variable is null as people have said before.

    However, you should use
    Code (Java):
    public File(File parent, String child)
    instead of using File.separator and combining them.
     

  5. I'm using this method to get the plugin variable (Hand typed as I don't have the code on me):



    Code (Text):
    /* In PanelFile class */
    private Core plugin = Core.getInstance();

    /* In Core class */

    private static Core instance;

    public void onEnable(){
    instance = this; /* This is first line of the method in actual code*/
    }

    public static Core getInstance(){
    return instance;
    }
     
    EDIT: It is null, I just tested to confirm. Wasn't even considering that before. I use this method to get the main instance every time and I've never had issues.
     
  6. Benz56

    Supporter

    Retrieve it in the method instead of directly in your field. That might fix it.
     
  7. Use JavaPlugin#getPlugin(Class) instead of creating that instance variable.
     
  8. This worked but I don't understand the difference? Can you explain?

    Already attempted this and didn't work
     
  9. Benz56

    Supporter

    I won’t go too much into it but your field is probably initialized before your onEnable resulting in retrieving the main instance before it has been instantiated.
     
  10. :rolleyes: Oh yep that's exactly why
     

Share This Page