Solved NPE on plugin.getConfig().set(... , ...);

Discussion in 'Spigot Plugin Help' started by LotuxPunk, Nov 14, 2017 at 8:00 PM.

  1. Hello !
    Main class :
    Code (Text):
    package com.vandendaelen.simpleautomessage;


    import java.io.File;
    import java.util.List;
    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;

    import com.vandendaelen.simpleautomessage.Commands.CommandSamRandom;
    import com.vandendaelen.simpleautomessage.Commands.CommandSamTime;

    public class SimpleAutoMessage extends JavaPlugin {
        public static final String RANDOM_CONFIG ="Random enabled";
        private int iMessages = 0;
       
        @Override
        public void onDisable() {
            // TODO Auto-generated method stub
            super.onDisable();
        }

        @Override
        public void onEnable() {
            System.out.println("Waw, an amazing plugin powered by LotuxPunk ! :-)");
            this.getCommand("samtime").setExecutor(new CommandSamTime(this));
            this.getCommand("samrandom").setExecutor(new CommandSamRandom(this, RANDOM_CONFIG));
            createConfig();
           
            this.getConfig().addDefault(RANDOM_CONFIG, false);
            this.getConfig().options().copyDefaults(true);
            saveConfig();
           
            //Enable display of messages
            if(getConfig().getBoolean("Enable")) {
                if(getConfig().getBoolean(RANDOM_CONFIG)) {
                    messageRandomDisplayer();
                } else {
                    messageDisplayer();
                }
            }
               
           
        }
       
        private void createConfig() {
            try {
                if (!getDataFolder().exists()) {
                    getDataFolder().mkdirs();
                }
                File file = new File(getDataFolder(), "config.yml");
                if (!file.exists()) {
                    getLogger().info("Config.yml not found, creating!");
                    saveDefaultConfig();
                } else {
                    getLogger().info("Config.yml found, loading!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       
        private void messageDisplayer() {
            List<String> listMessages = getConfig().getStringList("Messages");
            System.out.println(listMessages.size()+" messages loaded");
            BukkitScheduler scheduler = getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {

                @Override
                public void run() {
                   
                    for(Player player : Bukkit.getOnlinePlayers()) {
                        player.sendMessage(listMessages.get(iMessages));
                    }
                    iMessages++;
                    if (iMessages >= listMessages.size())
                        iMessages = 0;              
                }
               
            }, 0, this.getConfig().getInt("Time")*60*20);
        }
       
        private void messageRandomDisplayer() {
            Random randomGenerator = new Random();
            List<String> listMessages = getConfig().getStringList("Messages");
            System.out.println(listMessages.size()+" messages loaded");
            BukkitScheduler scheduler = getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {

                @Override
                public void run() {
                   
                    for(Player player : Bukkit.getOnlinePlayers()) {
                        player.sendMessage(listMessages.get(randomGenerator.nextInt(listMessages.size())));
                    }          
                }
               
            }, 0, this.getConfig().getInt("Time")*60*20);
        }
       
    }
     
    Command class :
    Code (Text):
    package com.vandendaelen.simpleautomessage.Commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    public class CommandSamRandom implements CommandExecutor {

        private String RANDOM_CONFIG;
        private Plugin plugin;

        public CommandSamRandom(Plugin pl, String r) {
            pl = plugin;
            RANDOM_CONFIG = r;
            //System.out.println(plugin.getConfig().getBoolean(RANDOM_CONFIG));
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if(args[0]!="") {
                Boolean randomEnabled = Boolean.parseBoolean(args[0]);
                if(p.hasPermission("simpleautomessage.setrandom")||p.isOp()) {
                    plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);
                   
                    if(randomEnabled) {
                        p.sendMessage("§2Random enabled");
                    } else {
                        p.sendMessage("§4Random disabled");
                    }
                    plugin.saveConfig();
                    plugin.reloadConfig();
                    return true;
                }
            }

            return false;
        }

    }
     
    Config file :
    Code (Text):
    #Time between 2 messages (minutes)
    Time: 15
    #Auto-Messages !
    Enable: true

    #Broadcast randomly your messages
    Random enabled: false

    Messages:
      - "A simple string"
      - "Another string"
      - "Don't forget to support/rate LotuxPunk on Curse/Bukkit website !"

     
    Result :
    Code (Text):
    [20:56:05 INFO]: LotuxPunk issued server command: /samrandom true
    [20:56:05 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'samrandom' in plugin SimpleAutoMessage v0.5
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:651) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_151]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_151]
            at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-d21162c-61e0c69]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]
    Caused by: java.lang.NullPointerException
            at com.vandendaelen.simpleautomessage.Commands.CommandSamRandom.onCommand(CommandSamRandom.java:26) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-d21162c-61e0c69]
            ... 15 more
    >
    Anyone can help me please ? :)
     
  2. The line 26 is
    Code (Text):
    plugin.getConfig().set(RANDOM_CONFIG, randomEnabled);
     
  3. Code (Text):
        private String RANDOM_CONFIG;
        private Plugin plugin;

        public CommandSamRandom(Plugin pl, String r) {
            plugin = pl;
            RANDOM_CONFIG = r;
        }
     

Share This Page