0

I'm recently I started making Minecraft plugins but I occurred some errors such as in this link https://paste.ubuntu.com/p/yHs2pQWf8t/

Main

package org.devoflua.hello;

import org.bukkit.plugin.java.JavaPlugin;
import org.devoflua.hello.commands.HelloCommand;

public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
        System.out.print("Okie");
        new HelloCommand(this);
    }
}

Command

package org.devoflua.hello.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.devoflua.hello.Main;

public class HelloCommand implements CommandExecutor {

    @SuppressWarnings("unused")
    private Main plugin;

    public HelloCommand(Main plugin) {
        this.plugin = plugin;
        plugin.getCommand("hello").setExecutor(this);
    }

    @Override
    public boolean onCommand(CommandSender Sender, Command Command, String label, String[] arg) {

        if (!(Sender instanceof Player)) {
            Sender.sendMessage("Only senders can use this command");
            return true;
        }

        Player p = (Player) Sender;

        if (p.hasPermission("hello.use")) {
            p.sendMessage("hi");
            return true;
        } else {
            p.sendMessage("You do not have permission to send this message");
        }

        return false;
    }
}

The error comes from the command class from line 16 I believe. I searched on the internet but I found nothing taht could help me fix this problem.

  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) There are only two things on line 16 that can be null: `plugin` and `plugin.getCommand("hello")`. – Draco18s no longer trusts SE Jul 10 '19 at 21:32
  • First of all, you should respect the conventions like variable/parameter naming. Also If the command isn't registered in the `plugin.yml` that error can occur and also I would not let the class set itself as `executor` to the command `hello`. So you should do that in the main class. – CodeMatrix Jul 11 '19 at 12:02

2 Answers2

0

So there are a few things you have to fix here because this won't work well.

First of all you have to use setExecutor() instead of creating a new instance of it. This can be done by adding this into your onEnable() :

this.getCommand("mycommand").setExecutor(new CommandKit());

You will have to specify this command in your plugin.yml as well, here is some documentation about it : https://www.spigotmc.org/wiki/plugin-yml/

Then you can delete your `HelloCommand()´, you won't need it anymore with what you changed above.

With this your error should go away, let me know if you still have some errors coming up.

Trisma
  • 651
  • 4
  • 18
  • He set the Executor... Inside of his HelloCommand-Class in the constructor there is `plugin.getCommand("hello").setExecutor(this);` so he did nothing wrong there in my opinion. His problem really might be that he did not register the command in his `plugin.yml` – Nicola Uetz Jul 18 '19 at 12:24
  • Yeah I know but it's a preference of working to have those in the Main class. And probably in `plugin.yml` yeah – Trisma Jul 18 '19 at 16:14
0

A NullPointerException means that at some point of your code an object or anything else is "null". Let me give you a good structure for your main but also your command class.

Main-Class:

package org.devoflua.hello;

import org.bukkit.plugin.java.JavaPlugin;
import org.devoflua.hello.commands.HelloCommand;

public class Main extends JavaPlugin {

public void onEnable() {
    System.out.println("Plugin Enabled");
    getCommand("hello").setExecutor(new HelloCommand();
    }

    public void onDisable() {
        System.out.println("Plugin Disabled");
    }
}

CommandExecutor class:

package org.devoflua.hello.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.devoflua.hello.Main;

public class HelloCommand implements CommandExecutor{

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (sender instanceof Player) {
        Player p = (Player) sender;
        if (p.hasPermission("hello.use")) {
            p.sendMessage("hi");
        } else {
            p.sendMessage("You do not have permission to send this message");
        }
    } else {
        sender.sendMessage("Only senders can use this command");
    }
    return true;
}
unknown
  • 1
  • 2