Error but what is wrong?

Discussion in 'Spigot Plugin Development' started by Creatos137, Apr 26, 2017.

  1. Hey,

    So I looked at my error and it said it was caused by the Main class line 42.
    But when I go and look it is this line:
    Code (Text):
    String header = plugin.getConfig().getString("Header");
    And Header is something in my config.yml file and that is this:
    Code (Text):
    Header: Info
     
  2. Post your stacktrace (error message) from console please.
     
  3. Code (Text):
    [15:55:08 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'info' in plugin Info v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-c3c767f-33d5de3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at com.creatos.Main.onCommand(Main.java:42) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-c3c767f-33d5de3]
            ... 14 more
     
  4. It's an issue with your onCommand method. Please send your 'Main' class file.
     
  5. Main:
    Code (Text):
    package com.creatos;

    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements CommandExecutor {

        private Main plugin;

        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();

            logger.info(pdfFile.getName() + "Has been enabled.");

            registerConfig();

        }

        public void onDisable() {

        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
            if (label.equalsIgnoreCase("info")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You must be a player to use this command");
                    return false;
                }

                Player player = (Player) sender;
                String blankLine = "";
                String spacing = "  ";
                String header = plugin.getConfig().getString("Header");
                String serverName = plugin.getConfig().getString("Server Name");
                String serverIp = plugin.getConfig().getString("Server Ip");
                String totalPlayers = plugin.getConfig().getString("Total Players");

                if (plugin.getConfig().getString("Blank Line").equals("true")) {
                    player.sendMessage(blankLine);
                }

                if (plugin.getConfig().getString("Is Header Enabled").equals("true")) {
                    player.sendMessage(spacing + ChatColor.BOLD + header);
                }

                if (plugin.getConfig().getString("Is Server Name Enabled").equals("true")) {
                    player.sendMessage(spacing + ChatColor.BOLD + "Server Name: " + ChatColor.GRAY + serverName);
                }

                if (plugin.getConfig().getString("Is Ip Name Enabled").equals("true")) {
                    player.sendMessage(spacing + ChatColor.BOLD + "Server IP: " + ChatColor.GRAY + serverIp);
                }

                if (plugin.getConfig().getString("Players Displayed").equals("true")) {
                    player.sendMessage(spacing + ChatColor.GRAY + getServer().getOnlinePlayers() + ChatColor.BOLD
                            + ChatColor.WHITE.toString() + totalPlayers);
                }

                if (plugin.getConfig().getString("Blank Line").equals("true")) {
                    player.sendMessage(blankLine);
                }

            }

            return false;
        }

        private void registerConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }

    }
    Config:
    Code (Text):
    ###########################################
    #                                         #
    #            Info, by Creatos             #
    #                                         #
    ###########################################


    # You can enable or disable every part by changing true to false or vice versa.
    # For more help contact me on Discord: Creatos#6143.

    #If you want a blank line on top and on the bottom set this to true.
    Blank Line: true

    Header: Info
    Is Header Enabled: true

    Server Name: My Server
    Is Server Name Enabled: true

    Server Ip: play.myserver.com
    Is Server Ip Enabled: true

    # Will display the amount of player online ( 12/20 )
    Total Players: 20
    Players Displayed: true
     
  6. Your code has a typo:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] arg) {
    It should be this (you typed 'args' as 'arg')
    Code (Text):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Fixing that should do it for you, because your plugin is having issues with initializing your onCommand method when it enables.
     
  7. This
    This still gives the same error!
     
  8. You never gave a value to the plugin field. Either do that in onEnable, or replace instances of that plugin with `this`
     
  9. You forgot to create your command In your onEnable method.

    Code (Text):
    getCommand("info").setExecutor(this);
    Edit: I've been told that it's not required if it's in your main class, but I don't think that's true.
     
  10. All of the things you're mentioning are either stylistic, or optional. They're contributing nothing to the problem the OP has
     
  11. Ah, I misunderstood the issue lol

    Try replacing "private Main plugin;" with "static JavaPlugin instance". Then do this:

    onEnable
    Code (Text):
    instance = this
    onDisable
    Code (Text):
    instance = null
    ...And for your getConfig methods, just do this instead:
    Code (Text):
    this.getConfig().getString() //etc
    This works every time for me, so give it a try. With this, you can also create a JavaPlugin method to retrieve your instance from another class for your config (or similar reasons).
     
  12. At that point you can just get rid of the instance variable, because you're never even using it.
     
  13. I clarified why I created that static instance just now at the bottom of my post. My method essentially creates an instance, and initializes it onEnable (disables it when necessary as well), so that you can retrieve an instance of your plugin whenever you need it. It's pretty much foolproof.
     
  14. What you're talking about can just as easily be accomplished with dependency injection via constructors. And that would be better practice than a public static variable
     
  15. lol
    There's no need to over-complicate anything when my method should easily solve the issue.

    @Creatos137 found a solution yet?
     
  16. Hmh no, it still says the same error :/
     
  17. Your method is bad practice that will lead to unmaintainable spaghetti code and that encourages bad programming practices in general.
     
  18. As I said before, replace all usages of the plugin variable with the 'this' keyword, and delete the plugin variable.
     
  19. this helped! Thank you
     
  20. For all of your checks for if a value is enabled, change them from Strings to booleans, and use name conventions (no spaces!) for your config variables.

    Also try enclosing all of your config Strings in quotes.