简体   繁体   English

Java枚举返回Null

[英]Java Enum Returns Null

I am currently creating a plugin for Minecraft using the SpigotAPI. 我目前正在使用SpigotAPI为Minecraft创建一个插件。 Reason I'm posting this here is because this I believe is a Java error. 我将其发布在这里的原因是因为我认为这是Java错误。 I am creating a duels plugin where inside my code it'll loop through an enum, and see if it's a specific type. 我正在创建一个Duells插件,该插件将在我的代码内遍历枚举,并查看它是否为特定类型。 The first time using it around it properly works, no problems. 第一次在它周围使用它可以正常工作,没有问题。 But when I try it for a second time without restarting my plugin/program/code it'll return the enum as null. 但是,当我第二次尝试而不重新启动插件/程序/代码时,它将把枚举返回为null。 Here is the code, is there a fix to this? 这是代码,对此是否有解决方法?

public DuelArenas[] getArenasWithType(DuelTypes type) {
    String suffix = "_NORMAL";
    List<DuelArenas> arenasAsList = new ArrayList<>();
    switch (type) {
        case NORMAL:
            suffix = "_NORMAL";
            break;
    }
    for (DuelArenas arena : duelArenaStatus.keySet()) {
        if (arena.toString().endsWith(suffix)) {
            arenasAsList.add(arena);
        }
    }
    DuelArenas[] arenas = new DuelArenas[arenasAsList.size()];
    return arenasAsList.toArray(arenas);
}

Stacktrace: 堆栈跟踪:

Caused by: java.lang.NullPointerException
    at me.nick.acore.duels.DuelsAPI.getArenasWithType(DuelsAPI.java:97) ~[?:?]

And yes I've checked to see if the enum was null, and it was in fact null. 是的,我检查了枚举是否为空,并且实际上是否为空。 Also line 97 is 第97行也是

if (arena.toString().endsWith(suffix)) {

And finally here is the DuelArena class 最后是DuelArena课程

public enum DuelArenas {

ARENA_1_NORMAL, ARENA_2_NORMAL, ARENA_3_NORMAL, ARENA_4_NORMAL, ARENA_5_NORMAL,
ARENA_6_NORMAL, ARENA_7_NORMAL, ARENA_8_NORMAL, ARENA_9_NORMAL, ARENA_10_NORMAL,
ARENA_11_NORMAL, ARENA_12_NORMAL }

Thanks! 谢谢!

Your problem is that you cannot directly convert your custom DuelArenas class to a string. 您的问题是您不能直接将自定义DuelArenas类转换为字符串。 However when you are comparing to see if a .toString() ends with suffix , I feel that something is also going wrong. 但是,当您进行比较以查看.toString()suffix结尾时,我觉得有些问题。 You would only ever use .toString to convert things like numbers to strings, and if your are converting a number to a string there is no way it will end in _NORMAL . 您只会使用.toString将数字转换为字符串,如果将数字转换为字符串,则无法以_NORMAL

So if you want me to troubleshoot further please post your DuelArenas class, but until then my best guess is that when you do arena.toString you are looking to pull some sort of value from that class that is stored in it, and to do this you would do arena.variableInsideArenaName and work with that. 因此,如果您希望我进行进一步的故障排除,请发布您的DuelArenas类,但是直到那时我最大的猜测是,当您执行arena.toString您正在寻找从存储在其中的该类中获取某种价值的方法,然后执行此操作您将执行arena.variableInsideArenaName并进行处理。

EDIT: 编辑:
After seeing the class scratch that, the error in going to be somewhere in this line DuelArenas arena : duelArenaStatus.keySet() 看到该类的内容后,该行的错误将出现在DuelArenas arena : duelArenaStatus.keySet()某处DuelArenas arena : duelArenaStatus.keySet()

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

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