Solved File location null

Discussion in 'Spigot Plugin Development' started by BocaRaton, May 17, 2018.

  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()) {

            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);

    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( ~[?:?]
        at com.bocaraton.carrotmc.Core.onEnable( ~[?:?]
        at ~[spigot.jar:git-Spigot-596221b-9a1fc1e]
        at [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init( [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at [spigot.jar:git-Spigot-596221b-9a1fc1e]
        at [?:1.8.0_161]
  2. Benz56

    Junior Mod 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

    Junior Mod 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

    Junior Mod 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