Solved NullPointerException when calling config.

Discussion in 'Plugin Development' started by Sethburster, May 17, 2016.

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

    Sethburster

    I am trying to call the config in my player listener:
    Code:
    package me.MC_Elmo.listeners;
    
    
    import me.MC_Elmo.Custom;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import static org.bukkit.ChatColor.*;
    
    
    
    public class PlayerJoin implements Listener
    {
        private Custom plugin;
        public PlayerJoin(Custom passedPlugin)
        {
    
            this.plugin = passedPlugin;
        }
        private String welomeMessage1 = plugin.config.getString("welcomeMessage1", RED + "Config file error!");
        private String welomeMessage2 = plugin.config.getString("welcomeMessage2", RED + "Config file error!");
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            Player player = event.getPlayer();
            player.sendMessage(welomeMessage1);
            player.sendMessage(welomeMessage2);
        }
    
    
    
    }
    
    
    When i try to run the plugin I get a nullPointer exception on line 22:
    Code:
     private String welomeMessage1 = plugin.config.getString("welcomeMessage1", RED + "Config file error!");
    `

    Here's the error:
    at me.MC_Elmo.Custom.<init>(Custom.java:18) ~[?:?]
    17.05 21:14:59 [Server] INFO at me.MC_Elmo.listeners.PlayerJoin.<init>(PlayerJoin.java:22) ~[?:?]
    17.05 21:14:59 [Server] INFO Caused by: java.lang.NullPointerException
     
  2. Offline

    Zombie_Striker

    @Sethburster
    You are trying to get a value from a class that is Null. Remember, things outside of the constructor are created first. This means you are using 'plugin' to get a config, before config is even set to anything.
     
    Sethburster likes this.
  3. Offline

    Sethburster

    Is there anyway to make sure the constructor is called first? Otherwise should i just call an instance of my main class outside the constructor?

    here is my main class:
    Code:
    package me.MC_Elmo;
    
    
    import me.MC_Elmo.command.gmCommand;
    import me.MC_Elmo.listeners.PlayerJoin;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.logging.Logger;
    
    public class Custom extends JavaPlugin {
    
        PluginDescriptionFile pdfFile = getDescription();
        Logger logger = getLogger();
        Listener playerJoinListener = new PlayerJoin(this);
        public static Custom plugin;
        public FileConfiguration config = getConfig();
    
        public void onEnable()
        {
            log("[MC_Elmo] Plugin Version " + pdfFile.getVersion() + " has been Enabled!");
            log("[MC_Elmo] " + pdfFile.getDescription());
            log("[MC_Elmo] Plugin by MC_Elmo");
            registerCommands();
            registerEvents();
            plugin = this;
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        public void onDisable()
        {
            log("[MC_Elmo] Plugin has been Disabled");
            plugin = null;
        }
    
        public void log(String msg)
        {
            logger.info(msg);
        }
    
        public void registerCommands()
        {
            this.getCommand("gm").setExecutor(new gmCommand(this));
        }
    
        public void registerEvents()
        {
            Bukkit.getServer().getPluginManager().registerEvents(playerJoinListener, this);
        }
    
    }
    


    {{Merged by moderator: Please use the Edit button next time}}
     
    Last edited by a moderator: May 17, 2016
  4. Offline

    Zombie_Striker

    @Sethburster
    The whole point of setting values outside of methods is so it will be set before any methods are called. Just set the welcome messages inside the classes constructor.
     
    Sethburster likes this.
  5. Offline

    Sethburster

    Thankyou :)
     
Thread Status:
Not open for further replies.

Share This Page