Solved Need help diagnosing a NPE

Discussion in 'Spigot Plugin Development' started by SpiroMarshes, May 30, 2016.

  1. Getting a nullpointerexception after adding a config file (or trying to). Here is the error I'm getting:
    Code (Text):
    30.05 18:00:19 [Server] INFO java.lang.NullPointerException
    30.05 18:00:19 [Server] INFO at com.spiromarshes.MystiqueCore.PlayerWelcomer.<init>(PlayerWelcomer.java:18) ~[?:?]
    30.05 18:00:19 [Server] INFO at com.spiromarshes.MystiqueCore.MystiqueMain.registerEvents(MystiqueMain.java:61) ~[?:?]
    30.05 18:00:19 [Server] INFO at com.spiromarshes.MystiqueCore.MystiqueMain.onEnable(MystiqueMain.java:26) ~[?:?]
    30.05 18:00:19 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]
    30.05 18:00:19 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_1.8.8_latest.jar:git-Spigot-e4d4710-e1ebe52]

    Here is my main class:
    Code (Text):
    package com.spiromarshes.MystiqueCore;

    import java.util.logging.Logger;

    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.economy.Economy;

    public class MystiqueMain extends JavaPlugin{
     
        private Vault vault;
        private Economy economy;
     
        public void onEnable(){
         
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
         
            logger.info(pdfFile.getName() + " has been enabled (V." + pdfFile.getVersion() + ")");
         
            registerConfig();
            registerEvents();
            registerCommands();
         
            this.vault = (Vault) this.getServer().getPluginManager().getPlugin("Vault");
            if (this.vault != null) {
                RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
                if (rsp != null) {
                    economy = rsp.getProvider();
                    this.getLogger().info("[Debug] Economy Enabled.");
                } else if (rsp == null) {
                    this.getLogger().info("[Debug] No economy plugin detected.");
                }
            }      

        }
     
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
         
            logger.info(pdfFile.getName() + " has been disabled (V." + pdfFile.getVersion() + ")");
            logger.info("Thanxxxxxxx K Bai");
        }
     
        private void registerConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
         
        }
     
        private void registerEvents()
        {
            PluginManager pm = getServer().getPluginManager();
         
            pm.registerEvents(new PlayerWelcomer(this), this);
         
        }
     
        private void registerCommands()
        {
            getCommand("blackjack").setExecutor(new BlackJack(this, economy));
         
        }
    }
     

    And here is my other class:
    Code (Text):
    package com.spiromarshes.MystiqueCore;

    import java.util.List;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scheduler.BukkitRunnable;

    public class PlayerWelcomer implements Listener
    {
     
        private MystiqueMain plugin;
     
        public List<String> sayings = plugin.getConfig().getStringList("Welcome Sayings");
     
        private final int MIN = plugin.getConfig().getInt("Minimum") * 20;
        private final int MAX = plugin.getConfig().getInt("Maximum") * 20;
     
     
        public PlayerWelcomer(MystiqueMain pl)
        {
            plugin = pl;
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent playerJoin)
        {
         
            Random rand = new Random();
            Player player = playerJoin.getPlayer();
         
            if(player.hasPlayedBefore() == false)
            {
                new BukkitRunnable()
                {
                    @Override
                    public void run()
                    {
                        for (Player player2: Bukkit.getServer().getOnlinePlayers())
                        {
                            if (player2.hasPermission("mystique.welcome") && player2 != player)
                            {
                                player2.chat("Welcome " + player.getName() + "!");
                            }
                        }                  
                    }  
                }.runTaskLater(this.plugin, randDelay());
            }
             
            if(player.hasPlayedBefore() == true)
            {
                 new BukkitRunnable()
                 {
                    @Override
                    public void run()
                    {
                        for (Player player2: Bukkit.getServer().getOnlinePlayers())
                        {
                            if (player2.hasPermission("mystique.welcomeback") && player2 != player)
                            {
                                player2.chat(sayings.get(rand.nextInt(sayings.size())));
                            }
                        }                      
                    }  
                }.runTaskLater(this.plugin, randDelay());
            }  
        }
     
        public int randDelay()
        {
            Random rand = new Random();
            int randomNum = rand.nextInt((MAX - MIN) + 1) + MIN;
            return randomNum;
        }
    }
     

    Don't make fun of the sloppy code, just doing this for learning purposes mainly.
     
  2. You're calling your plugin variable when initializing a field, but the plugin variable is null.

    Code (Java):
        private MystiqueMain plugin;

        public List<String> sayings = plugin.getConfig().getStringList("Welcome Sayings");
    Constructors are called after field initialization, so you would initialize "sayings" in the constructor.
     
    • Agree Agree x 1
  3. Assign sayings in the constructor after plugin = pl; You tried using plugin before it was initialized.
    [EDIT]
    Ninjaed by 1Rogue
     
    • Agree Agree x 1
  4. Move this code to your constructor after you have defined plugin.
    Code (Text):
    public List<String> sayings = plugin.getConfig().getStringList("Welcome Sayings");
    EDIT
    Whoops, way too late response, had this open in another tab :p @Ferdz and @1Rogue gave you the answer already :)
     
  5. Thank you guys :)
     
  6. Yup. agree with other posters above, your constructor has not
    established reference to the Main class that extends JavaPlugin yet.

    Like the others said, do that VVV

    Code (Text):
    public PlayerWelcomer(MystiqueMain pl)
        {
            plugin = pl;
            sayings = plugin.getConfig().getStringList("Welcome Sayings");
        }