[SOLVED] FileCannotBeNull! -- This is driving me crazy!!!

Discussion in 'Plugin Development' started by jis2507, Jan 14, 2012.

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

    jis2507

    Ok first of all, this is sort of a branch of my other thread "NullPointerException".
    However - I've solved that error....and nove I have another one :( I've search around for other people with this error - done everything other people have suggested, and yet I still mind up with a FileCannotBeNull error.
    Here's the code.
    ChatResponder.java
    Code:
    package me.jis2507.ChatResponder;
    
    import java.util.logging.Logger;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatResponder extends JavaPlugin {
    
        public FileConfiguration cfg;
    
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable() {
    
            if(this.getConfig().isSet("botName")) {
                cfg = getConfig();
                log.info("ChatResponder by jis2507 has been successfully enabled!");
                log.info(cfg.getString("botName"));
            } else {
                FileConfiguration cfg = this.getConfig();
                log.info("NO CONFIGURATION FILE FOUND!");
                cfg.set("botName", "<IMATROLLROBOT>");
                this.saveConfig();
                this.reloadConfig();
            }
    
            PluginManager pm  = this.getServer().getPluginManager();
    
            pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Event.Priority.Normal, this);
        }
    
        public void onDisable() {
            log.info("ChatResponder by jis2507 has been successfully disabled!");
        }
    
        private final ChatResponderPlayerListener playerListener = new ChatResponderPlayerListener(this);
    
    }
    
    And ChatResponderPlayerListener.java
    Code:
    package me.jis2507.ChatResponder;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class ChatResponderPlayerListener extends PlayerListener {
    
        public ChatResponder plugin;
        public String n;
    
        public ChatResponderPlayerListener(ChatResponder instance) {
            this.plugin = instance;
            this.n = plugin.getConfig().getString("botName", "<lol>");
        }
    
        public void onPlayerChat(PlayerChatEvent event) {
    
            String msg = event.getMessage();
            Player p = event.getPlayer();
            final String playerName = p.getName();
    
            if (msg.contains("lol")) {
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        Bukkit.broadcastMessage(ChatColor.BLUE + n + " Oi! " + playerName + "! What's so funny?!");
                    }
                }, 20L);
    
            }
    
            if (msg.contains(":D")) {
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        Bukkit.broadcastMessage(ChatColor.BLUE + n + " You look happy, " + playerName + "!");
                    }
    
                }, 20L);
    
            }
        }
    
    }
    
    And finally the stacktrace:
    Code:
    [SEVERE] Could not load 'plugins\ChatResponder.jar' in folder 'plugins':
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:175)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:215)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:136)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:151)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:127)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:52)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:145)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:399)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:229)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:131)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:125)
        at me.jis2507.ChatResponder.ChatResponderPlayerListener.<init>(ChatResponderPlayerListener.java:16)
        at me.jis2507.ChatResponder.ChatResponder.<init>(ChatResponder.java:44)
        ... 13 more
    Line 16 of ChatResponderPlayerListener is:
    Code:
    this.n = plugin.getConfig().getString("botName", "<lol>");
    ...and is inside the constructor for that class, which passes the main class instance over.
    This is driving me crazy! Every solution I found on the forum hasn't worked!! Hopefully somebody will be able to help me out! :D
    EDIT: Before somebody tells me that I can't use getConfig() inside of onEnable() (I've seen people saying this on the forum in various places), I can confirm that that part IS WORKING! It is only the part in the playerListener that is not.
    Thanks in advance!
     
  2. The problem is you're actually calling getConfig() BEFORE the plugin loads so it doesn't have a name yet and the file name is null.

    This line:
    Code:
    private final ChatResponderPlayerListener playerListener = new ChatResponderPlayerListener(this);
    Move it inside the onEnable() :)

    You shouldn't even do that because you don't use playerListener outside onEnable so you don't need it as global.

    Also, I dunno why you call reloadConfig() after saving it... that would most likely screw up your configs in certain situations :) see the source to understand.
     
  3. Offline

    jis2507

    THANKYOU!!!

    You have restored my faith in Bukkit!!!
    Well....it is always the user isn't it? :p
    But yeah thanks again! ...now I can finally continue work on this plugin.....
     
Thread Status:
Not open for further replies.

Share This Page