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.

2
  • Possible duplicate of 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"). Commented Jul 10, 2019 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. Commented Jul 11, 2019 at 12:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
Yeah I know but it's a preference of working to have those in the Main class. And probably in plugin.yml yeah
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;
}

Comments

0

There is a simple error in the Main class.

You're missing a closing parenthesis ) in the onEnable() method. The line:

getCommand("hello").setExecutor(new HelloCommand();    

should be:

getCommand("hello").setExecutor(new HelloCommand());    

Also, add @Override annotations for better practice:

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.println("Plugin Enabled");  
        getCommand("hello").setExecutor(new HelloCommand());  
    }  
    
    @Override  
    public void onDisable() {  
        System.out.println("Plugin Disabled");  
    }  
}

1 Comment

Thank you. That’s much better. Though I assume that was a typo in their question, not their code. If that were in their code, it wouldn’t have compiled, rather than throwing a null exception, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.