Solved NullPointerException

Discussion in 'Spigot Plugin Development' started by Ardno, Jan 23, 2020.

  1. Hello,
    I'm making a bank plugin, but I have got a problem.
    Code: (args[1] is String with random player's name)
    Code (Java):
    Player target = Bukkit.getPlayer(args[1]);
                                        try {
                                            boolean test = target.isOnline();
                                        } catch(NullPointerException e) {
                                            OfflinePlayer test = Bukkit.getOfflinePlayer(args[1]);
                                            if(plugin.getConfig().isSet(test.getName() + ".bank")) {
                                                String message = ChatUtil.format(plugin.prefixChat + plugin.getConfig().getString("messages.command.info.other").replace("%count%", plugin.getConfig().getString(target.getName() + ".bank")));
                                                message = message.replace("%player%", target.getName());
                                                p.sendMessage(message);
                                                break;
                                            }
                                            p.sendMessage(ChatUtil.format(plugin.prefixChat + plugin.getConfig().getString("messages.command.error.info.othernoset")));
                                            break;
                                        }
    Error:
    Code (Text):
    [19:50:58] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tokensbank' in plugin TokensBank v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:910) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:903) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:887) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:820) [spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
    Caused by: java.lang.NullPointerException
            at cz.ardno.tokensbank.commands.TokensBank.onCommand(TokensBank.java:35) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.14.4.jar:git-Spigot-9de398a-9c887d4]
            ... 17 more
    And what I want. I want to test, if player(mentions in args[1]) isn't online and is set in config, send message to player(CommandSender) with balance of player(args[1]). How can I repair this?
     
  2. Post this.
    Bte, your way of checking if the player is online is terrible, you can just use:
    if player!=null
     
    #2 robertlit, Jan 23, 2020
    Last edited: Jan 23, 2020
  3. Bukkit#getPlayer() is annotated Nullable, so it can (naturally) return null.
     
    • Agree Agree x 1
  4. You have to actually check whether "target" is null or not.
    So just remove your try-catch block and replace it with something like the following:

    Code (Java):
    Player target = Bukkit.getPlayer(args[1]);
    final boolean isOnline = target != null;
    if (isOnline) ...
    else ...
     
    btw you can easily get the OfflinePlayer first, you don't actually have a need to get the online player for database purposes.
     
    • Winner Winner x 1