Solved Getting scoreboard tags in InventoryClickEvent give a EventException: null error

Discussion in 'Spigot Plugin Development' started by MCrafterzz, Aug 10, 2018.

  1. Code:
    Code (Text):
    if (event.getSlot() == 8) {
                    Bukkit.getPlayer("MCrafterzz").sendMessage(machine.getUniqueId().toString());
                    if (machine.getScoreboardTags() != null) {
                        for (String tag : machine.getScoreboardTags()) {
                            if (tag.contains("Facing")) {
                                machineGui.setItem(8, changeFacing(tag));
                                machine.getScoreboardTags()
                                        .add("Facing" + GetNextFacing(tag.replace("Facing", "")).toUpperCase());
                                machine.getScoreboardTags().remove(tag);
                                continue;
                            }
                        }
                    }
    It crashes on for (String tag : machine.getScoreboardTags()) { even tho I check if getScoreboardTags is null. Machine is not null as the message always prints.

    [09:09:56 ERROR]: Could not pass event InventoryClickEvent to MCrafterzzMachines v1.0.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:308) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.PlayerConnection.a(PlayerConnection.java:2130) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.PlayerConnectionUtils.a(SourceFile:10) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
    at net.minecraft.server.v1_13_R1.SystemUtils.a(SourceFile:198) [spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.MinecraftServer.w(MinecraftServer.java:884) [spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.DedicatedServer.w(DedicatedServer.java:411) [spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.MinecraftServer.v(MinecraftServer.java:819) [spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at net.minecraft.server.v1_13_R1.MinecraftServer.run(MinecraftServer.java:717) [spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextNode(Unknown Source) ~[?:?]
    at java.util.HashMap$KeyIterator.next(Unknown Source) ~[?:?]
    at mcrafterzz.machines.gui.MachineGui.onInventoryClick(MachineGui.java:127) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:?]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:?]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304) ~[spigot-1.13.jar:git-Spigot-ed1cec9-1c7adf7]
    ... 15 more
     
  2. what is the machine variable? please provide the whole class
     
  3. The problem here is not a NullPointerException but a ConcurrentModificationException.
    You are changing the scoreboard tags while iterating over it. Even more, while are you adding the continue statement at the end of the if? There is no need for it.
     
  4. The significant information is at "Caused by":
    Code (Text):
    Caused by: java.util.ConcurrentModificationException
    You're editing scoreboard tags while iterating over them. Java's HashMap doesn't like that. Make a copy and iterate over that, and you should be good.
    Code (Text):
    for (String tag: new ArrayList<>(machine.getScoreboardTags())) { ... }
    I'm actually a bit confused why it's a HashMap complaining while scoreboard tags are a list... perhaps I'm missing something.
    It's a HashSet. Mystery solved.
     
    • Like Like x 1
  5. It's because getScoreboardTags return a HashSet. Sets are based on Maps, you can get now why a HashMap is mentioned inside the stacktrace.
     
  6. Thx that solved it