Solved Why is this null?

Discussion in 'Spigot Plugin Development' started by gluebaby, May 25, 2017.

  1. Code (Text):
    [22:04:32 INFO]: Undead_Guppy issued server command: /eg create test
    [22:04:32 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'eg' in plugin gEnderGames v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
        at me.undeadguppy.gEnderGames.arena.Game.arenaExists(Game.java:40) ~[?:?]
        at me.undeadguppy.gEnderGames.cmd.ArenaCreate.onCommand(ArenaCreate.java:22) ~[?:?]
        at me.undeadguppy.gEnderGames.cmd.CommandManager.onCommand(CommandManager.java:56) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    >
     

    Code (Text):
        return a != null && a.getName().equals(name);
     
    • Funny Funny x 1
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    I believe && works by checking both, and even if one is false it will still check the other.
    For example, if a == null, then doing "a != null && a.function()" will give a nullpointer.
     
  3. Code (Text):
    public boolean arenaExists(String name) {
            if (!arenas.isEmpty()) {
                for (Arena a : arenas) {
                    if (a != null) {
                        if (a.getName().equals(name)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    Changed to that, and the error is on line 42 (getName().equals(name).
     
  4. Code (Text):
      if (a != null) {
    should probably be
    Code (Text):
      if (a.getName() != null) {
    because a.getName() is the variable you're using.
     
  5. Now it just allows me to create multiples of the same arena....
     
  6. Code (Text):
    return a != null && a.getName() != null && a.getName().equals(name);
    It will only keep checking if the first checks are true. As soon as a false is found it stops checking and returns false, avoiding any npe.
     
    • Informative Informative x 1
  7. That makes sense, but I believe it was passing those checks yet the arena was still null. I'm worried because I may have even more issues with my code, specifically serialization/deserialization so I'll keep trying, also @ChefJava ill try that later.

    @Sharkbait why was my post funny? (If it's the package names, those were like that a while back, and I haven't gotten around to fixing them.)
     
  8. Agreeing with BillyGalbreath. && is not bitwise. It will stop evaluating expressions the moment a false appears. In your case, a is not null but maybe a.getName() is? You can't run .equals(Object) on null. It just doesn't work.

    Nevermind. I now realise that it's already been answered. Mark your thread as solved if you feel that's appropriate. Reading the post above, I suppose you've got some more problems. Fire away! (Maybe in another thread? Keeps it friendly for other people to find their answers - the forum is supposed to act like a knowledgebase)
     
  9. I haven't tested anything yet (I'm at school) so I'll leave it open for a little bit and then post an update. Either way I might switch to Gson instead of yaml.

    For everyone, I guess you could call those Semi-Solved.
     
  10. We can't call it anything. You can though! :>
    When you get back, be sure to update the thread to [Solved] if appropriate.
     
    • Agree Agree x 1
  11. getName() is likely the one returning null, but a name shouldn't right? I think you got a bit of a problem there. :p
     
    • Agree Agree x 1
  12. Yeah, I believe it's serialiazation issues. Here what my file is outputting:
    Code (Text):

    arenas:
      'null':
        reqkills: 10
        name: null
        reqplayers: 2
        spawns: []
     
    EDIT: Hold on, I may be stupid af.

    EDIT 2: Fixed, I'm actually an idiot. I forgot to initialize the name field ;-;.
     
  13. This is why you should be using Kotlin,

    It would be

    Code (Text):
    a?.name == name
     
    • Optimistic Optimistic x 2
    • Like Like x 1
    • Agree Agree x 1
  14. I found out what was wrong with it, nothing to do with kotlin.
     
  15. I never said the problem had anything to do with Kotlin, just that Kotlin allows for easier, cleaner, and clearer syntax involving nullability and access.
     
    • Like Like x 1
  16. Not sure why people are rating this optimistic, it works and it improves the experience of programming on the JVM 200%. Have an agree rating