new File() causes NullPointerException

Discussion in 'Plugin Development' started by rtainc, Jan 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    rtainc

    I've got a bug I cannot find out how to fix. It's perfect code, but I cannot get it to work.

    Code:
    Code:
    13    File file = new File(getDataFolder(), "Config.yml");
    14    FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    15    public void saveDefaults() {
    16        if(!file.exists()) {
    17            try {
    18                file.createNewFile();
    19            } catch (IOException e) {
    20                getLogger().severe("Could not create Config.yml! " + e);
    21            }
    22        }
    23    }
    
    Here's the error I receive:
    Code:
    [SEVERE] Error occurred while enabling iMonies v2.1 (Is it up to date?)
    java.lang.NullPointerException
        at com.mciseries.iMonies.system.ConfigFile.<init>(ConfigFile.java:13)
    
     
  2. Offline

    ZachBora

    Try
    File file = new File(getDataFolder().getAbsolutePath(), "Config.yml");

    Are you declaring a file outside a function?
    Can you profile your whole code
     
  3. Offline

    drtshock

    File file = new File(getDataFolder() + File.separator + "config.yml");
     
  4. Offline

    rtainc

    This is the whole ConfigFile class:
    Code:
    package com.mciseries.iMonies.system;
     
    import java.io.File;
    import java.io.IOException;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
     
    import com.mciseries.iMonies.iMonies;
     
    public class ConfigFile {
        private iMonies plugin;
        File file = new File(plugin.getDataFolder(), "Config.yml");
        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
        public void saveDefaults() {
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    plugin.getLogger().severe("Could not create Config.yml! " + e);
                }
            }
            if(file.exists()) {
                this.setDouble("Options.DefaultBalance", 100.00);
                this.setString("Options.SingularName", "Dollar");
                this.setString("Options.PluralName", "Dollars");
                this.setString("Messages.Prefix", "iMonies");
                this.setString("Messages.Money", "Balance: $<balance>");
                this.setString("Messages.Set", "Changed balance to $<balance>");
                this.setString("Messages.Create", "Created account named <name>");
                this.setString("Messages.Pay", "Payed <name> $<amount>");
                this.setString("Messages.Empty", "Deleted all accounts");
                this.setString("Messages.Remove", "Deleted <name>''s account");
                this.setString("Messages.Errors.NoPermission", "No permission");
                this.setString("Messages.Errors.NotEnoughMoney", "Not enough money");
                this.setString("Messages.Errors.NegativeAmount", "Can''t pay amounts less than a cent");
                this.setString("Messages.Errors.NotEnoughArgs", "Not enough arguments");
                this.setString("Messages.Errors.TooManyArgs", "Too many arguments");
            }
        }
        public void setDouble(String path, double value) {
            config.set(path, value);
        }
        public void setString(String path, String value) {
            config.set(path, value);
        }
        public void setInt(String path, int value) {
            config.set(path, value);
        }
        public void setBoolean(String path, boolean value) {
            config.set(path, value);
        }
    }
    
    I'm calling saveDefaults() with:
    Code:
    new ConfigFile().saveDefaults();
     
  5. Offline

    drtshock

    Paid* :eek:
     
    camyono likes this.
  6. Offline

    ZachBora

    rtainc
    You never set your plugin variable. You should add

    public ConfigFile(iMonies instance){
    plugin = instance;
    }
     
    drtshock likes this.
  7. Offline

    rtainc

    Oh, so basically plugin is a null variable? Huh. I'll try it now, thanks.

    Wow, failed spelling of paid. :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page