Solved i need some help (java beginner)

Discussion in 'Spigot Plugin Development' started by CollinBoy2001, Aug 18, 2018.

  1. i keep getting this error:

    Code (Text):

    [19:26:02] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'cordsadd' in plugin VenetiusCore v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at org.bukkit.craftbukkit.v1_13_R1.CraftServer.dispatchCommand(CraftServer.java:692) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.PlayerConnection.handleCommand(PlayerConnection.java:1634) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.PlayerConnection.a(PlayerConnection.java:1469) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.PlayerConnectionUtils.a(SourceFile:10) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
        at net.minecraft.server.v1_13_R1.SystemUtils.a(SourceFile:198) [spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.MinecraftServer.w(MinecraftServer.java:884) [spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.DedicatedServer.w(DedicatedServer.java:411) [spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.MinecraftServer.v(MinecraftServer.java:819) [spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at net.minecraft.server.v1_13_R1.MinecraftServer.run(MinecraftServer.java:717) [spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
        at collin.venetiuscore.commands.CommandAddCord.onCommand(CommandAddCord.java:26) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.13.jar:git-Spigot-a85f7ec-5870145]
        ... 15 more

     
    Here is my code for the command:

    Code (Text):

    package collin.venetiuscore.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import net.md_5.bungee.api.ChatColor;
    import collin.venetiuscore.Main;
    import collin.venetiuscore.util.ListStore;


    public class CommandAddCord implements CommandExecutor {
       
        private Main plugin;
        public ListStore cords;

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (args.length != 1) {
                sender.sendMessage(ChatColor.RED + "Usage: /cords add <Location Name>");
                return true;
            }
            Player player = Bukkit.getPlayer(label);
            plugin.cords.add(player.getLocation().getBlock().getLocation()+"  "+args[0]);
            sender.sendMessage(ChatColor.GREEN + "position '" + args[0] + "has been added to the list.");
            this.cords.save();
            return true;
        }

    }
     
    please help
     
  2. Easy as everything. You forgot to inject your plugin instance by using a constructor.
     
  3. sorry if i sound dumb as you know i am a noob but how do i inject my plugin instance?
     
  4. You have "private Main plugin;" but it's not set to anything, do this:
    Code (Java):
    private Main plugin; //This is what you have

    public CommandAddCord(Main plugin) { //Make a constructor
        this.plugin = plugin; //Set your plugin variable to an instance of your main class
    }
    Hope i explained it well enough.
     
  5. Create a constructor with your plugin as a parameter and assign the parameter to your attribute.
     
  6. now my main class is saying: "The constructor CommandAddCord() is undefined"

    main:
    Code (Text):

    package collin.venetiuscore;

    import java.io.File;

    import org.bukkit.plugin.java.JavaPlugin;

    import collin.venetiuscore.commands.CommandCords;
    import collin.venetiuscore.commands.CommandAddCord;
    import collin.venetiuscore.util.ListStore;

    public class Main extends JavaPlugin {
     
        public ListStore cords;
     
        @Override
        public void onEnable() {
         
            String pluginFolder = this.getDataFolder().getAbsolutePath();
         
            (new File(pluginFolder)).mkdirs();
         
            this.cords = new ListStore(new File(pluginFolder + File.separator + "cords.txt"));
         
            this.cords.load();
         
            this.getCommand("cords").setExecutor(new CommandCords());
            this.getCommand("cordsadd").setExecutor(new CommandAddCord());
            System.out.println("[VenetiusCore] Enabled");
        }
     
        @Override
        public void onDisable() {
            this.cords.save();
            System.out.println("[VenetiusCore] Disabled");
        }
    }
     
    Code (Text):

    package collin.venetiuscore.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import net.md_5.bungee.api.ChatColor;
    import collin.venetiuscore.Main;
    import collin.venetiuscore.util.ListStore;


    public class CommandAddCord implements CommandExecutor {
     
        private Main plugin;

        public CommandAddCord(Main plugin) {
            this.plugin = plugin;
        }
        public ListStore cords;

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (args.length != 1) {
                sender.sendMessage(ChatColor.RED + "Usage: /cords add <Location Name>");
                return true;
            }
            Player player = Bukkit.getPlayer(label);
            plugin.cords.add(player.getLocation().getBlock().getLocation()+"  "+args[0]);
            sender.sendMessage(ChatColor.GREEN + "position '" + args[0] + "has been added to the list.");
            this.cords.save();
            return true;
        }

    }
     
     
  7. In your onEnable change this:
    Code (Java):
    this.getCommand("cordsadd").setExecutor(new CommandAddCord());
    to this:
    Code (Java):
    this.getCommand("cordsadd").setExecutor(new CommandAddCord(this));
     
  8. Creeoer

    Supporter

    You are missing a constructor argument in your main. You have to do new CommandAddCord(this) as that is how you defined the constructor.

    Also:
    System.out.println("[VenetiusCore] Enabled"); is not a necessary line of code as Bukkit already handles enabling and disabling messages for you, it was necessary in the past however it isn't now.
     
  9. okay guys it's working now thnx for helping a beginner out
     
  10. Glad i could help. :7
     
  11. Just think of a constructor like a big with one return type (which will always be the class type itself), so the constructor is where you wanna make sure everything you wanna define/do before you really start using it is done in there.. And be sure to mark the thread as solved :b