Solved 'plugin' variable is null in constructor

Discussion in 'Plugin Development' started by Jake_0403, Jan 5, 2021.

  1. Offline

    Jake_0403

    Hello,

    I have been trying to make a TDM plugin, however, in one of my constructors the plugin variable keeps return null and I do not know why, I do the same thing in all of my other classes and they have no issue.

    here is my stacktrace:
    Code:
    05.01 17:21:57 [Server] INFO Enabling Minigame1 v1.0.0
    05.01 17:21:57 [Server] ERROR Error occurred while enabling Minigame1 v1.0.0 (Is it up to date?)
    05.01 17:21:57 [Server] INFO java.lang.NullPointerException
    05.01 17:21:57 [Server] INFO at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:523) ~[spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at me.Jake_0403.MiniGame1.listeners.UpdateGameSB.<init>(UpdateGameSB.java:23) ~[?:?]
    05.01 17:21:57 [Server] INFO at me.Jake_0403.MiniGame1.scoreboards.GameScoreboard.<init>(GameScoreboard.java:26) ~[?:?]
    05.01 17:21:57 [Server] INFO at me.Jake_0403.MiniGame1.listeners.ChatFormat.<init>(ChatFormat.java:16) ~[?:?]
    05.01 17:21:57 [Server] INFO at me.Jake_0403.MiniGame1.TDM.onEnable(TDM.java:26) ~[?:?]
    05.01 17:21:57 [Server] INFO at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot46.jar:git-Spigot-21fe707-e1ebe52]
    05.01 17:21:57 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211]
    I know there are errors in other classes but those are caused because I reference the UpdateGameSB class (the one throwing the NPE) in those classes.

    So I know what and where my problem is but I have no idea how to fix it.

    here is my main class, TDM:
    Code:
    package me.Jake_0403.MiniGame1;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.Jake_0403.MiniGame1.listeners.ChatFormat;
    import me.Jake_0403.MiniGame1.listeners.GameStart;
    import me.Jake_0403.MiniGame1.listeners.PlayerJoinAndWait;
    import me.Jake_0403.MiniGame1.listeners.UpdateGameSB;
    import me.Jake_0403.MiniGame1.listeners.UpdateLobbySB;
    import me.Jake_0403.MiniGame1.scoreboards.GameScoreboard;
    import me.Jake_0403.MiniGame1.scoreboards.LobbyScoreboard;
    import me.Jake_0403.MiniGame1.utils.Utils;
    
    public class TDM extends JavaPlugin {
    
        public void onEnable() {
            saveDefaultConfig();
            getServer().getMessenger().registerOutgoingPluginChannel(this, "BuneeCord");
            GameStart.gameStarted = false;
            Bukkit.getServer().getWorld("world").setFullTime(0);
    
            // Commands
    
            // Listeners
            new ChatFormat(this);
            new GameStart(this);
            new PlayerJoinAndWait(this);
            new UpdateGameSB(this);
            new UpdateLobbySB(this);
    
            // GUIs
    
            // Scoreboards
            new GameScoreboard(this);
            new LobbyScoreboard(this);
    
            // Utils
            new Utils(this);
        }
    }
    And lastly the class that is throwing the error, UpdateGameSB:
    Code:
    package me.Jake_0403.MiniGame1.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    
    import me.Jake_0403.MiniGame1.TDM;
    import me.Jake_0403.MiniGame1.scoreboards.GameScoreboard;
    
    public class UpdateGameSB implements Listener {
    
        private TDM plugin;
        public int redKills;
        public int blueKills;
        public int playerKills;
    
        public UpdateGameSB(TDM plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    I can post the full class if needed.
    Any advice is welcome
    Thanks!!
     
  2. Offline

    Kars

    You are calling the constructor for UpdateGameSB from GameScoreboard apparently.
    Show that class (GameScoreBoard).
     
  3. Offline

    Jake_0403

    @Kars
    Code:
    package me.Jake_0403.MiniGame1.scoreboards;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.NameTagVisibility;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
    
    import me.Jake_0403.MiniGame1.TDM;
    import me.Jake_0403.MiniGame1.listeners.UpdateGameSB;
    import me.Jake_0403.MiniGame1.utils.Utils;
    
    public class GameScoreboard {
    
        private TDM plugin;
        public static Scoreboard gameSB;
        public Team red;
        public Team blue;
      
        Utils utils = new Utils(plugin);
        LobbyScoreboard lobbyScoreboard = new LobbyScoreboard(plugin);
        UpdateGameSB updateGameSB = new UpdateGameSB(plugin);
      
        public GameScoreboard(TDM plugin) {
            this.plugin = plugin;
        }
    
        public void gameScoreboard() {
      
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            setGameSB(manager.getNewScoreboard());
          
            Objective objective = getGameSB().registerNewObjective("PlayTime!", "");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(utils.chat("&cTDM"));
          
            setRed(getGameSB().registerNewTeam("Red"));
            red.setDisplayName(ChatColor.RED + "RED");
            red.setAllowFriendlyFire(false);
            red.setCanSeeFriendlyInvisibles(true);
            red.setNameTagVisibility(NameTagVisibility.ALWAYS);
          
            setBlue(getGameSB().registerNewTeam("Blue"));
            blue.setDisplayName(ChatColor.BLUE + "BLUE" + ChatColor.RESET);
            blue.setAllowFriendlyFire(false);
            blue.setCanSeeFriendlyInvisibles(true);
            blue.setNameTagVisibility(NameTagVisibility.ALWAYS);
    
    
            Score discordLink = objective.getScore("discord.gg/BfWzRxfr");
            discordLink.setScore(1);
          
            Score blank2 = objective.getScore("");
            blank2.setScore(2);
          
            Score blueTeam = objective.getScore(ChatColor.BLUE + "Blue: " +ChatColor.WHITE + String.valueOf(updateGameSB.getBlueKills()));
            blueTeam.setScore(3);
          
            Score redTeam = objective.getScore(ChatColor.RED + "Red: " +ChatColor.WHITE + String.valueOf(updateGameSB.getRedKills()));
            redTeam.setScore(4);
          
            Score blank5 = objective.getScore("");
            blank5.setScore(5);
    
            Score playerKill = objective.getScore("Kills: " + String.valueOf(updateGameSB.getPlayerKills()));
            playerKill.setScore(6);
          
            setGameSB(getGameSB());
        }
      
        //SETTERS
        public static void setGameSB(Scoreboard scoreboard) {
            gameSB = scoreboard;
        }
    
        public void setRed(Team team) {
            this.red = team;
        }
      
        public void setBlue(Team team) {
            this.blue = team;
        }
      
        //GETTERS
        public static Scoreboard getGameSB() {
            return gameSB;
        }
    
        public Team getRed() {
            return this.red;
        }
      
        public Team getBlue() {
            return this.blue;
        }
    }
    
    And also yes, I called the class UpdateGameSB inside of GameScoreboard.
     
    Last edited: Jan 5, 2021
  4. Offline

    Kars

    @Jake_0403 this is your problem here:
    PHP:
    Utils utils = new Utils(plugin);
    LobbyScoreboard lobbyScoreboard = new LobbyScoreboard(plugin);
    UpdateGameSB updateGameSB = new UpdateGameSB(plugin);
    You are initiating these at class-level with the variable plugin, which is null.

    Initiate these in the constructor instead of at class level.
     
  5. Offline

    Jake_0403

    @Kars
    So I got it to work! I did it a little differently than you suggested, but I still really appreciated your perspective, it will help me in the future.

    When you said that I was calling the constructor from GameScoreboard I decided to move the variables 'redKills' 'blueKills' and 'playerKills' to the GameScoreboard class instead of having them in updateGameSB.

    By doing this I don't have to reference any class in order to get those variables.

    this is what my classes looked like in the end:

    updateGameSB:
    Code:
    package me.Jake_0403.MiniGame1.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    
    import me.Jake_0403.MiniGame1.TDM;
    import me.Jake_0403.MiniGame1.scoreboards.GameScoreboard;
    
    public class UpdateGameSB implements Listener {
    
        private TDM plugin;
      
        GameScoreboard gameScoreboard = new GameScoreboard(plugin);
    
        public UpdateGameSB(TDM plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
    
        @EventHandler
        public void redKill(PlayerDeathEvent e) {
            if (GameStart.gameStarted == true) {
                if (((GameScoreboard) GameScoreboard.getGameSB()).getBlue()
                        .hasEntry(e.getEntity().getPlayer().getName()) == true) {
                    gameScoreboard.setRedScore(gameScoreboard.getRedScore() + 1);
    
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        p.setScoreboard(GameScoreboard.getGameSB());
                    }
                } else {
                    return;
                }
            } else {
                return;
            }
        }
    
        @EventHandler
        public void blueKill(PlayerDeathEvent e) {
            if (GameStart.gameStarted == true) {
                if (((GameScoreboard) GameScoreboard.getGameSB()).getRed()
                        .hasEntry(e.getEntity().getPlayer().getName()) == true) {
                    gameScoreboard.setBlueScore(gameScoreboard.getBlueScore() + 1);
    
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        p.setScoreboard(GameScoreboard.getGameSB());
                    }
                } else {
                    return;
                }
            } else {
                return;
            }
        }
    
        @EventHandler
        public void playerKill(PlayerDeathEvent e) {
            Player p = ((Player) e).getPlayer();
    
            if (p.getKiller() == null) {
                return; //this is temporary
    //Use a list to make a collection of all the EntityDamageEvents and then check to see if the DamageEvent.getPlayer == DeathEvent.getPlayer, then check if DamageEvent.getDamgageCause() instanceof Entity, if it is, DeathEvent.getPlayer().getKiller() == DamgeEvent.getDamgeCause().getEntity().getPlayer();
            } else { // (if p.getKiller() != null) {
                Player killer = p.getKiller();
    
                gameScoreboard.setPlayerScore(killer, gameScoreboard.getPlayerScore() + 1);
                killer.setScoreboard(GameScoreboard.getGameSB());
            }
        }
    }
    gameScoreboard:
    Code:
    package me.Jake_0403.MiniGame1.scoreboards;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.NameTagVisibility;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
    
    import me.Jake_0403.MiniGame1.TDM;
    import me.Jake_0403.MiniGame1.utils.Utils;
    
    public class GameScoreboard {
    
        private TDM plugin;
        public static Scoreboard gameSB;
        public Team red;
        public Team blue;
        public int redScore;
        public int blueScore;
        public int playerScore;
      
        Utils utils = new Utils(plugin);
      
        public GameScoreboard(TDM plugin) {
            this.plugin = plugin;
        }
    
        public void gameScoreboard() {
      
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            setGameSB(manager.getNewScoreboard());
          
            Objective objective = getGameSB().registerNewObjective("PlayTime!", "");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(utils.chat("&cTDM"));
          
            setRed(getGameSB().registerNewTeam("Red"));
            red.setDisplayName(ChatColor.RED + "RED");
            red.setAllowFriendlyFire(false);
            red.setCanSeeFriendlyInvisibles(true);
            red.setNameTagVisibility(NameTagVisibility.ALWAYS);
          
            setBlue(getGameSB().registerNewTeam("Blue"));
            blue.setDisplayName(ChatColor.BLUE + "BLUE" + ChatColor.RESET);
            blue.setAllowFriendlyFire(false);
            blue.setCanSeeFriendlyInvisibles(true);
            blue.setNameTagVisibility(NameTagVisibility.ALWAYS);
    
    
            Score discordLink = objective.getScore("discord.gg/BfWzRxfr");
            discordLink.setScore(1);
          
            Score blank2 = objective.getScore("");
            blank2.setScore(2);
          
            Score blueTeam = objective.getScore(ChatColor.BLUE + "Blue: " +ChatColor.WHITE + String.valueOf(blueScore));
            blueTeam.setScore(3);
          
            Score redTeam = objective.getScore(ChatColor.RED + "Red: " +ChatColor.WHITE + String.valueOf(redScore));
            redTeam.setScore(4);
          
            Score blank5 = objective.getScore("");
            blank5.setScore(5);
    
            Score playerKills = objective.getScore("Kills: " + String.valueOf(playerScore));
            playerKills.setScore(6);
          
            setGameSB(getGameSB());
        }
      
        //SETTERS
        public static void setGameSB(Scoreboard scoreboard) {
            gameSB = scoreboard;
        }
    
        public void setRed(Team team) {
            this.red = team;
        }
      
        public void setBlue(Team team) {
            this.blue = team;
        }
      
        public void setBlueScore(int kills) {
            this.blueScore = kills;
        }
      
        public void setRedScore(int kills) {
            this.redScore = kills;
        }
      
        public void setPlayerScore(Player player, int kills) {
            Objective objective = player.getScoreboard().getObjective("objective");
            Score playerKills = objective.getScore("playerKills");
            kills = Integer.valueOf(playerKills.getEntry());
            this.playerScore = kills;
        }
      
        //GETTERS
        public static Scoreboard getGameSB() {
            return gameSB;
        }
    
        public Team getRed() {
            return this.red;
        }
      
        public Team getBlue() {
            return this.blue;
        }
      
        public int getBlueScore() {
            return this.blueScore;
        }
      
        public int getRedScore() {
            return this.redScore;
        }
      
        public int getPlayerScore() {
            return playerScore;
        }
    }
    Thanks for the help!!
     

Share This Page