Question Making commands class file results in nullpointerexception when starting up causing plugin to be disabled

pikachu1

New member
Mar 6, 2022
3
0
1
i got this code
package me.pikachu.random2.commands;

import me.pikachu.random2.Main;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class Commands implements CommandExecutor {
protected Main plugin;

public Commands(Main plugin) {
this.plugin = plugin;
plugin.getCommand("gift-on").setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player p = (Player) sender;
Material mat = p.getInventory().getItemInMainHand().getType();
World w = p.getWorld();
Location loc = p.getLocation();
Inventory inv = p.getInventory();

if(mat == Material.PEONY) {
p.sendMessage("You already have the peony, lust click it!");

}else {
inv.addItem(new ItemStack(Material.PEONY));
p.sendMessage("click the peony");
}
return false;
}
}


It seems that this line causes the problem: plugin.getCommand("gift-on").setExecutor(this);