-1

My Command (Specifically only my cmd2 command) doesn't register, and the console displays an error when I start the server. The other command, cmd1, works, but cmd2 doesn't. I'm really not sure why, so I came here for help.

Some of my Main Class:

package me.Vamp.Test;

import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

    private Commands commands = new Commands();

    @Override
    public void onEnable() {
        /* Enabler */
        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest Plugin has been enabled.\n\n");
        /* Events Register */
        getServer().getPluginManager().registerEvents(new EventsClass(), this);
        /* Commands Register */
        getCommand(commands.cmd1).setExecutor(commands);
        getCommand(commands.cmd2).setExecutor(commands);
    }
}

The following Class (Commands) only show for the errored command (cmd2). If the code for cmd1 is needed, I will show it. Some of My Command Class:

package me.Vamp.Test;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;

public class Commands implements Listener, CommandExecutor {

    public String cmd2 = "getpickaxe";

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {

            /*
            /getPickaxe Command
            */

            if (cmd.getName().equalsIgnoreCase(cmd2)) {
                Player player = (Player) sender;
                if (args.length == 0) {
                    commandGetPickaxe(player);
                    return true;
                } else {
                    player.sendMessage(Colors.chat("&c&lERROR &cToo many arguments&8."));
                    return true;
                }
            }

        } else {

            sender.sendMessage(Colors.chat("&c&lERROR &cOnly players can use this command&8."));

            return true;
        }
        return false;
    }

    public void commandGetPickaxe(Player player){
        Inventory inv = player.getInventory();
        ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
        ItemMeta meta = item.getItemMeta();
        ArrayList<String> lore = new ArrayList<String>();

        meta.setDisplayName(Colors.chat("&3Wooden Pickaxe"));
        lore.add(Colors.chat("&7&oThe Starter Pickaxe&8&o."));
        meta.setLore(lore);
        item.setItemMeta(meta);

        inv.addItem(new ItemStack(item));
        player.sendMessage(Colors.chat("&8&l» &3You have received a Wooden Pickaxe&8."));
    }
}

This is only the display error on my console.

My Console:

Console

Vamp
  • 65
  • 1
  • 7
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Joseph Sible-Reinstate Monica Mar 16 '20 at 21:00
  • Sorry, but that isn't related to Bukkit or Minecraft. The error has to do with the Bukkit code, I'm pretty sure. – Vamp Mar 16 '20 at 22:28

1 Answers1

0

Can I suggest you add a multitude of print statements to see what is null?

/* Commands Register */
System.out.println("cmd1 " + commands.cmd1);
System.out.println("cmd2 " + commands.cmd2);
System.out.println("cmdObj " + commands);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);

EDIT 1: It seems as though you are missing the command in your plugin.yml? It's possible that it is a typo, look carefully. If you think everything is perfectly fine, and the error still occurs, please edit you original post and include the plugin.yml file. Thanks!

TCoded
  • 413
  • 4
  • 11
  • Here you go: [11:55:13 INFO]: cmd1 testplugin [11:55:13 INFO]: cmd2 getpickaxe [11:55:13 INFO]: cmdObj me.Vamp.Test.Commands@58d58c1c [11:55:13 ERROR]: (Same exact error as before) – Vamp Mar 17 '20 at 15:57
  • Also, command 1 (cmd1) works, its just command 2 (cmd2) that has problems while trying to execute. – Vamp Mar 17 '20 at 15:59
  • I have successfully reproduced your error btw, now let me find a fix @Vamp – TCoded Mar 17 '20 at 16:10
  • I think I found the answer, Let me check this out, I will update the post in 2 seconds :) – TCoded Mar 17 '20 at 16:20
  • Thanks for answering my question! I completely forgot to add it to my plugin.yml, so thank you for reminding me! :) – Vamp Mar 17 '20 at 16:35