NullPointerException - No idea what went wrong!

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

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

    jis2507

    Basically, I've been playing around with making some plugins and such, and I wanted to try out the configuration file system, so I decided to learn about it, by implementing it into one of my test plugins.
    But I keep on getting NullPointerException. I know what it means, but I don't know why its happening :(
    Eclipse isn't giving me any areas, and I've gone over it quite a few times but it just won't work. If I comment out the troublesome line, then it works fine, so long as I redefine the variable elsewhere. You'll see what I mean in a minute.
    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 {
    
        private final ChatResponderPlayerListener playerListener = new ChatResponderPlayerListener(this);
    
        Logger log = Logger.getLogger("Minecraft");
      
        public void onEnable() {
    
            if(this.getConfig().isSet("botName")) {
                FileConfiguration cfg = this.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>");
                saveConfig();
            }
    
            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!");
        }
    
    }
    
    ChatResponderPlayerListener.java
    Code:
    package me.jis2507.ChatResponder;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    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 ChatResponderPlayerListener(ChatResponder instance) {
            plugin = instance;
        }
    
        FileConfiguration lol = this.plugin.getConfig();
    
        String n = lol.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);
    
            }
        }
    
    }
    
    The error is being return at line 18 of ChatResponderPlayerListener. (FileConfiguration lol = this.plugin.getConfig();)
    I've tried getting rid of "this" and just going plugin.getConfig etc.
    I've tried doing things directly such as:
    Code:
    String name = plugin.getConfig().getString("botName");
    But nothing helps.
    The only thing that helps is doing:
    Code:
    String name = "name_of_bot";
    But that completely defeats the purpose :(
    -----------------------------------------
    Before you ask:
    Yes, I've gone over the code myslef extensively.
    Yes, I've looked at the wiki documentation.
    Yes, I've looked at various configuration guides.
    Yes, I know what a NullPointerException is.
    Yes, I've searched for threads about similar problems.
    Yes, I know how to interpret a stack-trace.
    --------------------------------
    Anybody know what the problem is?
    Thanks is advance!

    Thanks for the reply!
    But it still isn't working!
    I get the same error, except this time, its for line 21 which this time is:
    Code:
    String n = lol.getString("botName", "<lol>");
    I assume that that means lol is still null? Why would lol still be null?!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  2. Offline

    Seadragon91

    Sorry that I had deleted my post. That line is null to, because lol is at null.
    If you call a class all variables inside a class not inside of methods, will be initialized, that mean at that moment plugin is at null.

    Code:
    public ChatResponderPlayerListener(ChatResponder instance) {
            plugin = instance;
            lol  = this.plugin.getConfig();
            n = lol.getString("botName", "<lol>");
        }
    
        FileConfiguration lol;
    
        String n;
    This should work.
     
  3. Offline

    jis2507

    Sorry, but that doesn't work either :(
    I'm getting a different error when I use your idea.
    The error is: IllegalArgumentException: File cannot be Null.
    The line that causes the error is:
    Code:
    lol = this.plugin.getConfig();
    ...which is part of the code you suggested.
    Has ANYBODY got a clue on what I'm doing wrong? SO MANY plugins use configs, so surely SOMEBODY can show me what I've done thats throwing this error??
    Thanks.
     
  4. Offline

    Seadragon91

    You have to load the config too:
    cfg.load(new File(this.getDataFolder(), "config.yml"));

    This is the onEnable() with that line:
    Code:
    public void onEnable() {
            if(this.getConfig().isSet("botName")) {
                FileConfiguration cfg = this.getConfig();
                cfg.load(new File(this.getDataFolder(), "config.yml")); // load the file
                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>");
                saveConfig();
            }
            PluginManager pm  = this.getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Event.Priority.Normal, this);
        }
     
  5. Offline

    jis2507

    Still not working!!
    Besides, according to the wiki, you don't need to load a file, if you're just going to be using the default "config.yml" file , as it will default to that....
    Anybody got ideas on what's going wrong?
     
  6. dont call getConfig() from a consturctor, only cal it after or inside the onLoad() methode
     
  7. Calling getConfig() from constructors is perfectly fine as long as the constructor (class instance beeing created) is called after onEnable() triggered so that the file name is not null !

    Still, as I replied here: http://forums.bukkit.org/threads/still-unsolved-filecannotbenull-this-is-driving-me-crazy.54748/ the issue was that his extension of PlayerListener class was called in the main class init point, and then the getConfig() didn't have a file name.
     
  8. it was called while the JavaPlugin class was stil constructing, and thats the bad thing
     
  9. Yes, that, but your last reply can be understood that you shouldn't use getConfig() from any class' constructor, which is false :p just by moving that piece of code inside onEnable() the plugin will work.
     
Thread Status:
Not open for further replies.

Share This Page