getObjective returns null

Discussion in 'Plugin Development' started by SuperMegaRorro, Aug 24, 2021.

  1. Offline

    SuperMegaRorro

    I have a problem with getObjective, here is my code:
    Code:
    package com.rorro.rpgmagic.commands;import org.bukkit.Bukkit;import org.bukkit.command.Command;import org.bukkit.command.CommandExecutor;import org.bukkit.command.CommandSender;import org.bukkit.command.ConsoleCommandSender;import org.bukkit.entity.Player;import org.bukkit.potion.PotionEffect;import org.bukkit.potion.PotionEffectType;import org.bukkit.scoreboard.Objective;import org.bukkit.scoreboard.Score;import org.bukkit.scoreboard.Scoreboard;import org.bukkit.scoreboard.ScoreboardManager;public class MagicCommands implements CommandExecutor {
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if (!(sender instanceof Player)) {
    sender.sendMessage("[RPG Magic] This is not console command.");
    return true;}
    Player player = (Player) sender;String playername = player.getName();
    ConsoleCommandSender console = Bukkit.getServer().getConsoleSender(); if (cmd.getName().equalsIgnoreCase("speedme")) {
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getNewScoreboard();System.out.println("test " + board);
    // [21:21:20 INFO]: test org.bukkit.craftbukkit.v1_17_R1.scoreboard.CraftScoreboard@58390a78
    Objective objective = board.getObjective("off1");System.out.println("test " + objective);
    // [21:21:20 INFO]: test nullScore usersScore = objective.getScore(playername);
    sender.sendMessage("[RPG Magic] This is not console command." + usersScore);
    int sc = usersScore.getScore(); if (sc > 0) {
    int a = 10 + (5 * sc); if (sc > 3) {
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, a, 1));}
    if (sc <= 3) {
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, a, 0));}
    }
    }
    return true;}
    }
    
    when I type the / speed command on the console I get:
    Code:
    [21:21:20 INFO]: SuperMegaRorro issued server command: /speed
    [21:21:20 INFO]: test org.bukkit.craftbukkit.v1_17_R1.scoreboard.CraftScoreboard@58390a78
    [21:21:20 INFO]: test null
    [21:21:20 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'speed' in plugin RPGMagic v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.17.1.jar:git-Paper-103]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.17.1.jar:git-Paper-103]
            at org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchCommand(CraftServer.java:821) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:2170) ~[?:?]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1981) ~[?:?]
            at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1962) ~[?:?]
            at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:46) ~[?:?]
            at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:6) ~[?:?]
            at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$1(PacketUtils.java:36) ~[?:?]
            at net.minecraft.server.TickTask.run(TickTask.java:18) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:149) ~[?:?]
            at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:23) ~[?:?]
            at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1348) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.server.MinecraftServer.shouldRun(MinecraftServer.java:190) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:122) ~[?:?]
            at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1327) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1320) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:132) ~[?:?]
            at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1397) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1188) ~[patched_1.17.1.jar:git-Paper-103]
            at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:316) ~[patched_1.17.1.jar:git-Paper-103]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.scoreboard.Objective.getScore(String)" because "objective" is null
            at com.rorro.rpgmagic.commands.MagicCommands.onCommand(MagicCommands.java:35) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.17.1.jar:git-Paper-103]
            ... 21 more
     
    Last edited by a moderator: Aug 24, 2021
  2. Offline

    man_in_matrix

    ok, #1 thing your code is really hard to read especially with the imports all in one line. (IDK if this is an error on your side or if the forums formatted it like that but either edit or repost the code.

    And also in the first line of your console error is says null this means that a piece of information somewhere is missing.

    Right here it tells you the issue. The objective is null so try adding a null check or add a value to objective before running the command :D

    also use try {
    } catch (exceptionName e) {
    e.printStackTrace();
    }
    It will make your errors alot more readable
    [put the name of the exception insread of exeptionName, in your case it would be
    tey {
    // code here
    } catch (NullPointerException e) {
    e.printStackTrace();
    }

    Hope this helps
     
  3. Offline

    SuperMegaRorro

    Console:
    Code:
    [11:11:08 INFO]: SuperMegaRorro issued server command: /speed
    [11:11:08 INFO]: test BELOW_NAME
    [11:11:08 INFO]: test []
    [11:11:08 WARN]: java.lang.NullPointerException: Cannot invoke "org.bukkit.scoreboard.Objective.getScore(String)" because "objective" is null
    [11:11:08 WARN]:        at com.rorro.rpgmagic.commands.MagicCommands.onCommand(MagicCommands.java:36)
    [11:11:08 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)
    [11:11:08 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
    [11:11:08 WARN]:        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchCommand(CraftServer.java:821)
    [11:11:08 WARN]:        at net.minecraft.server.network.PlayerConnection.handleCommand(PlayerConnection.java:2170)
    [11:11:08 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1981)
    [11:11:08 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1962)
    [11:11:08 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:46)
    [11:11:08 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:6)
    [11:11:08 WARN]:        at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$ensureRunningOnSameThread$1(PlayerConnectionUtils.java:36)
    [11:11:08 WARN]:        at net.minecraft.server.TickTask.run(TickTask.java:18)
    [11:11:08 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:149)
    [11:11:08 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(IAsyncTaskHandlerReentrant.java:23)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:1348)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:190)
    [11:11:08 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:122)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1327)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1320)
    [11:11:08 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:132)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1281)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1192)
    [11:11:08 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:316)
    [11:11:08 WARN]:        at java.base/java.lang.Thread.run(Thread.java:831)
    Edited code, and in lines as requested.

    Code:
    package com.rorro.rpgmagic.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scoreboard.*;
    
    public class MagicCommands implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("[RPG Magic] This is not console command.");
                return true;
            }
            Player player = (Player) sender;
            String playername = player.getName();
            ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
    
            try {
            if (cmd.getName().equalsIgnoreCase("speedme")) {
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                System.out.println("test " + DisplaySlot.values()[0].name());
                // [21:21:20 INFO]: test org.bukkit.craftbukkit.v1_17_R1.scoreboard.CraftScoreboard@58390a78
                Objective objective = board.getObjective("off1");
                //Objective objective = board.getObjective(DisplaySlot.values()[0]);
                System.out.println("test " + board.getObjectives());
                // [21:21:20 INFO]: test null
    
                Score usersScore = objective.getScore(playername);
                sender.sendMessage("[RPG Magic] This is not console command." + usersScore);
                int sc = usersScore.getScore();
                if (sc > 0) {
                    int a = 10 + (5 * sc);
                    if (sc > 3) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, a, 1));
                    }
                    if (sc <= 3) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, a, 0));
                    }
                }
            }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            return true;
        }
    }
    
    For this code I used this forum:
    https://bukkit.org/threads/get-player-scoreboard-value-into-a-variable-string.406161/

    So how can i fix it?
     
    Last edited: Aug 30, 2021
  4. Offline

    SuperMegaRorro

  5. Offline

    Shqep

    @SuperMegaRorro
    The error message is really good and tells exactly the problem already:
    Cannot invoke "org.bukkit.scoreboard.Objective.getScore(String)" because "objective" is null

    PHP:
    // Getting the manager, so far so good.
    ScoreboardManager manager Bukkit.getScoreboardManager();

    // Notice the method invocation here, you wanted a NEW scoreboard.
    Scoreboard board manager.getNewScoreboard();

    // Retrieve an objective called "off1" from the NEWLY created scoreboard.
    Objective objective board.getObjective("off1");

    // Objective was not found, so "objective" is currently null. Trying to use a null pointer, and there's the exception.
    Score usersScore objective.getScore(playername);
    Pretty sure this is the issue?
     

Share This Page