简体   繁体   中英

NoSuchMethod Error Java but method exists

I understand there are other topics on this already but their solutions have not successfully been able to help me. I am creating a plugin and there is a class called CommandErrors that has only static references and methods in it. Here is the error printing out in the console:

Console Error

SEVERE: null

 org.bukkit.command.CommandException: Unhandled exception executing command 'capturetheflag' in plugin CaptureTheFlag v1.0<br /> at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)<br /> at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)<br /> at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:543)<br /> at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:923)<br /> at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:803)<br /> at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28)<br /> at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47)<br /> at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146)<br /> at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)<br /> at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:645)<br /> at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:243)<br /> at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:535)<br /> at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:447)<br /> at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)<br /> Caused by: java.lang.NoSuchMethodError: <br />net.strikecraft.Commands.CommandErrors.throwError(Lorg/bukkit/command/CommandSender;Lnet/strikecraft/Commands/CTFCommandExecutor;Ljava/lang/String;)Ljava/lang/String;<br /> at net.strikecraft.Commands.CTFCommand.onCommand(CTFCommand.java:28)<br /> at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)<br /> ... 13 more 


CTFCommand.onCommand()

 package net.strikecraft.Commands;

 import java.util.ArrayList;

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

 public class CTFCommand implements CommandExecutor {

private static ArrayList<CTFCommandExecutor> commands = new ArrayList<CTFCommandExecutor>();
public static CTFCommandExecutor registerCommand(CTFCommandExecutor cmd){
    commands.add(cmd);
    return cmd;
}
public static ArrayList<CTFCommandExecutor> getCommands(){
    return commands;
}

public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    if(!(sender instanceof Player)){
        CommandErrors.throwError(sender, null, CommandErrors.NOT_A_PLAYER);
        return true;
    }
    Player player = (Player)sender;
    if(args.length == 0){
        CommandErrors.throwError(player, null, CommandErrors.COMMAND_NOT_FOUND);
        return true;
    }
    CTFCommandExecutor ctfCommand = null;
    for(CTFCommandExecutor c : commands){
        if(c.command().equalsIgnoreCase(args[0]))
            ctfCommand = c;
    }
    if(ctfCommand == null){
        CommandErrors.throwError(player, null, CommandErrors.COMMAND_NOT_FOUND);
        return true;
    }
    if(ctfCommand.permission() != null && !player.hasPermission(ctfCommand.permission())){
        CommandErrors.throwError(sender, ctfCommand, CommandErrors.NO_PERMISSION);
        return true;
    }
    if(ctfCommand.validArgsCount() != -1 && args.length != ctfCommand.validArgsCount()){
        CommandErrors.throwError(sender, ctfCommand, CommandErrors.INCORRECT_USAGE);
        return true;
    }
    String[] ctfCommandArgs = new String[args.length-1];
    for(int x=1; x<args.length; x++){
        ctfCommandArgs[x-1] = args[x];
    }
    ctfCommand.onCommand(player, ctfCommandArgs);
    return true;
}

 }

CommandErrors

 package net.strikecraft.Commands;

 import net.strikecraft.CaptureTheFlag;

 import org.bukkit.ChatColor;
 import org.bukkit.command.CommandSender;

 public class CommandErrors {

public static String throwError(CommandSender sender, CTFCommandExecutor subCmd, String error){
    if(error.startsWith("#")){
        String hashError = null;
        if(error.equals(INCORRECT_USAGE)){
            hashError = "Incorrect usage. Type /ctf "+subCmd.usage();
            sender.sendMessage(CaptureTheFlag.PREFIX+ChatColor.RED+hashError);
        }
        return hashError;
    }

    String errorMsg = CaptureTheFlag.PREFIX+ChatColor.RED+error;
    sender.sendMessage(errorMsg);
    return error;
}

public static final String NOT_A_PLAYER = "You must be a player in order to perform this action.";

public static final String NO_PERMISSION = "You do not have permission to this command.";
public static final String COMMAND_NOT_FOUND = "Command not found. Type /ctf help";
public static final String GAME_NOT_FOUND = "Game not found.";
public static final String GAME_ALREADY_EXISTS = "A game with this name already exists.";

public static final String INCORRECT_USAGE = "#incorrect-usage";

 }

How can I fix this NoSuchMethod error? I know for sure the method and class does exist. Thanks!

I figured out not the problem but a solution to the problem. Although I'm sure it's definitely better programming practice and just in general better to fix the problem itself the proper way but

Hope this helps others!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM