Solved NullPointerException on trying to make a file

Discussion in 'Plugin Development' started by AlfieJay, May 24, 2020.

  1. Offline

    AlfieJay

    So I've been struggling to make a file for my plugin to save data, I keep getting the NullPointerException (on the line marked with ##)

    Separate class method:
    Code:
        static void load() {
            File file = new File(plugin.getDataFolder()+File.separator+"data.yml"); ##
            //FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }   
        }
    Main class onEnable:
    Code:
        public void onEnable() {
            FILEMANAGER.load(); ##
            PluginManager pm = getServer().getPluginManager();
            LISTENER listener = new LISTENER(this);
            pm.registerEvents(listener, this);
            plugin = this;
            CONFIGURATION.loadConfig();
        }
    Please help if you know how I can fix this. Thanks a bunch :D
     
  2. Offline

    timtower Moderator Moderator

    @AlfieJay plugin is null.
    Solution: don't use static config handlers. Make an instance that takes the main class as parameter in the constructor.
     
  3. Offline

    AlfieJay

    Thanks so much for the help, it works fine now :D
     

Share This Page