简体   繁体   English

NoSuchMethod错误Java但存在方法

[英]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. 我正在创建一个插件,并且有一个名为CommandErrors的类,其中只包含静态引用和方法。 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() 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 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? 我该如何解决这个NoSuchMethod错误? 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 I have found that creating a class with a different name but with the same body fixes the problem. 虽然我确信它绝对是更好的编程实践,并且通常更好地以正确的方式解决问题本身,但我发现创建一个具有不同名称但具有相同主体的类可以解决问题。

Hope this helps others! 希望这有助于他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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