Strange crash NullPointerException

Discussion in 'Plugin Development' started by Niko3DGames, May 6, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I got a strange crash when I execute a public void() methode by a schedueler and execute another in it.
    Crash report:

    Code:
    Task #11 for TTT v1.0 generated an exception
    java.lang.NullPointerException: null
            at niko.ttt.gamestates.IngameState.start(IngameState.java:22) ~[?:?]
            at niko.ttt.gamestates.GameStateManager.setGameState(GameStateManager.java:19) ~[?:?]
            at niko.ttt.countdowns.LobbyCountdown$1.run(LobbyCountdown.java:42) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-3d850ec-809c399]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [spigot.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-3d850ec-809c399]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-3d850ec-809c399]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_171]
    this will execute by the schedueler:

    Code:
        @Override
        public void start() {
            for(Player current : main.getPlugin().getInGame()) {
                if(rm.getPlayerRole(current) == null) {
                    rm.setPlayerRole(current, role.values()[new Random().nextInt(role.values().length)]);
                    current.sendMessage(main.PREFIX + "§aDir wurde eine Rolle hinzugefügt.");
                  
                }
                current.sendMessage(main.PREFIX + "§bDeine Rolle: §6" + rm.getPlayerRole(current).getRoleName() + "§b.");
              
                main.getPlugin().getScoreboards().score(current);
            }
            cdstart();
        }

    only the cdstart(); methode is crashing! When I remove it it works perfectly. (the cdstart(); methode is empty)


    sry for my english :(

    Edit: I imported this workspace, because I developted on another PC.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: May 7, 2018
  2. Offline

    MightyOne

    You can highlight you code by inserting a piece of code btw.

    What exactly is your cdstart-method doing? Please post your full code so someone can help you.
     
  3. Code:
        private boolean isRunning = false,
                        isIdling = false,
                        isStarting = false;
      
        private int seconds = 15;
        private final int RESET_SECONDS = 15,
                          IDLE_SECONDS = 20;
      
      
        @Override
        public void run() {
            isRunning = true;
            cancelIdle();
            taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getPlugin(), new Runnable() {
              
                @Override
                public void run() {
                    switch(seconds) {
                    case 60: case 30: case 15: case 10: case 5: case 4:case 2:
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6" + seconds + " Sekunden§a.");
                        break;
                    case 3:
                        isStarting = true;
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6" + seconds + " Sekunden§a.");
                        break;
                    case 1:
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6einer Sekunde§a.");
                        break;
                    case 0:
                        main.getPlugin().getGameStateManager().setGameState(GameState.INGAME_STATE);
                        break;
                    default:
                        break;
                    }
                  
                    seconds--;
                }
            }, 0, 20* 1);
            seconds--;
        }
    
        @Override
        public void cancel() {
            isRunning = false;
          
            Bukkit.getScheduler().cancelTask(taskID);
            seconds = RESET_SECONDS;
        }
    with "main.getPlugin().getGameStateManager().setGameState(GameState.INGAME_STATE);"
    the LobbyState will be stopped and the IngameState will be started.
    It does not matter what I write in the public void start() from the IngameState, it gives this crash every time.
     
    Last edited: May 7, 2018
  4. Offline

    MightyOne

    Hey, you wrote your program and nobody else knows what your methods are doing. So if you want help from anyone, you have to post everything one needs to understand your code.

    Whats the purpose of main.getPlugin().GetGameStateManager().SetGameState (GameState.INGAME_STATE);?
    What does it affect? What exactly could be null there?
     
  5. So this is the GameStateManager class where the GameState will set and where I can get the CurrentGameState.

    Code:
    public class GameStateManager {
      
        private GameState[] gameStates = new GameState[3];
        private GameState currentGameState;
      
        public GameStateManager() {
            gameStates[GameState.LOBBY_STATE] = new LobbyState();
            gameStates[GameState.INGAME_STATE] = new IngameState();
            gameStates[GameState.ENDING_STATE] = new EndingState();
        }
      
        public void setGameState(int gameStateIndex) {
            if(currentGameState != null)
                stopCurrentGameState();
          
            currentGameState = gameStates[gameStateIndex];
            currentGameState.start();
        }
      
        public void stopCurrentGameState() {
            currentGameState.stop();
            currentGameState = null;
        }
      
        public GameState getCurrentGameState() {
            return currentGameState;
        }
    }

    The abstract GameState class includes the IndexIDS and State names.
    Code:
    public abstract class GameState {
      
        public static final int LOBBY_STATE = 0,
                                INGAME_STATE = 1,
                                ENDING_STATE = 2;
      
        public abstract void start();
        public abstract void stop();
    
    }
    The LobbyState class includes what happens in the LobbyState (Start methode, when LobbyState starts; Stop methode when its stop)
    Disclaimer: the lobbystate is starting in the onEnable part in the main class.

    Code:
    public class LobbyState extends GameState {
    
        public static final int MIN_PLAYERS = 1,
                                MAX_PLAYERS = 13;
      
        private LobbyCountdown lobbyCountdown;
    
        @Override
        public void start() {
            lobbyCountdown = new LobbyCountdown();
        }
        @Override
        public void stop() {
            Bukkit.broadcastMessage("§6Die Runde beginnt!");
        }
        public LobbyCountdown getLobbyCountdown() {
            return lobbyCountdown;
        }
    }
    The IngameState is Start when the countdown is at 0 in the LobbyCountdown class.
    Disclaimer: It does not matter what I write in the start methode or stop methode(from LobbyState) it sends the crash anyway.

    Code:
    public class IngameState extends GameState {
    
        private IngameCountdown ingameCountdown;  
      
        @Override
        public void start() {
            ingameCountdown = new IngameCountdown();
            RoleManager rm = main.getPlugin().getRoleManager();
            for(Player current : main.getPlugin().getInGame()) {
                if(rm.getPlayerRole(current) == null) {
                    rm.builder(current);
                    current.sendMessage(main.PREFIX + "§7Deine Rolle wird sein: " + rm.getPlayerRole(current).getRoleName() + "§7.");
                }
    
                managePlayer(current);
              
                main.getPlugin().getScoreboards().score(current);
            }
            ingameCountdown.run();
          
        }
    
        @Override
        public void stop() {
            getIngameCountdown().cancel();
        }
      
        public IngameCountdown getIngameCountdown() {
            return ingameCountdown;
        }
    And the countdown class from the LobbyState:

    Code:
    public class LobbyCountdown extends countdown {
    
        private boolean isRunning = false,
                        isStarting = false;
      
        private int seconds = 15;
        private final int RESET_SECONDS = 15;
      
      
        @Override
        public void run() {
            isRunning = true;
            cancelIdle();
            taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getPlugin(), new Runnable() {
              
                @Override
                public void run() {
                    switch(seconds) {
                    case 60: case 30: case 15: case 10: case 5: case 4:case 2:
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6" + seconds + " Sekunden§a.");
                        break;
                    case 3:
                        isStarting = true;
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6" + seconds + " Sekunden§a.");
                        break;
                    case 1:
                        Bukkit.broadcastMessage(main.PREFIX + "§aDas Spiel startet in §6einer Sekunde§a.");
                        break;
                    case 0:
                        main.getPlugin().getGameStateManager().setGameState(GameState.INGAME_STATE);
                        break;
                    default:
                        break;
                    }
                  
                    seconds--;
                }
            }, 0, 20* 1);
        }
    
        @Override
        public void cancel() {
            isRunning = false;
          
            Bukkit.getScheduler().cancelTask(taskID);
            seconds = RESET_SECONDS;
        }
        public void setSeconds(int seconds) {
            this.seconds = seconds;
        }  
        public boolean isStarting() {
            return isStarting;
        }
      
        public boolean isRunning() {
            return isRunning;
        }
    the Countdown abstract class where the every countdown class will extend:
    Code:
    public abstract class countdown {
        protected int taskID,
                      idleID,
                      ingameID;
    
        public abstract void run();
        public abstract void cancel();
    }

    The main class :)
    Code:
    public class main extends JavaPlugin {
        private static main plugin;
        public static final String PREFIX = "§7[§4§lTTT§7] ";
      
        private GameStateManager GameStateManager;
        private RoleManager roleManager;
        private ArrayList<Player> inGame;
    
        private scoreboards scoreboards;
      
        @Override
        public void onEnable() {
            plugin = this;
            GameStateManager = new GameStateManager();
            inGame = new ArrayList<>();
            GameStateManager.setGameState(GameState.LOBBY_STATE);
            roleManager = new RoleManager();
            scoreboards = new scoreboards();
          
            init(); // Register Events and Commands.
          
            Bukkit.getConsoleSender().sendMessage(PREFIX + "§aPlugin geladen.");
        }  
        public static main getPlugin() {
            return plugin;
        }
    
        public GameStateManager getGameStateManager() {
            return GameStateManager;
        }
      
        public ArrayList<Player> getInGame() {
            return inGame;
        }
      
        public RoleManager getRoleManager() {
            return roleManager;
        }
      
        public scoreboards getScoreboards() {
            return scoreboards;
        }
    }
    I imported this workspace from another PC.
     
  6. Offline

    MightyOne

    @Niko3DGames it isn't exactly clear what is going wrong. The error says line 22 in IngameState throws a nullpointerexception. Since you cut out the imports idk which line this is. I just assume that you try to invoke a method of an object that doesn't exist. Otherwise the error would also list the line of code from another class. changing anything in the lobbyStaze won't help
     
  7. Offline

    JRL1004

    @Niko3DGames So, this is a bit of a mess, but what I am seeing is this:
    Code:java
    1.  
    2. if(rm.getPlayerRole(current) == null) { // Ensure that the player's role is null
    3. rm.builder(current);
    4. current.sendMessage(main.PREFIX + "§7Deine Rolle wird sein: " + rm.getPlayerRole(current).getRoleName() + "§7."); // Try to get the role name of a null value
    5. }

    The problem with this is that the check ensure that getPlayerRole() is returning null, but your sendMessage line seems to ignore that, as it calls getPlayerRole().getRoleName(), which guarantees a NPE. This matches the reasoning that @MightyOne provided as well.

    Edit: Unless rm.builder() fixes this issue, this is a likely problem in your code

    Edit 2: Note that this code snippet is from the start method of your IngameState class
     
    MightyOne likes this.
  8. Offline

    MightyOne

    @Niko3DGames did you now understand how to read an exception message?
    I'm just wondering why it took you 3 attempts to post the code where the exception is pointing at and that you told you tried to solve the problem at a totally different part of code which had nothing to do with it.
     
Thread Status:
Not open for further replies.

Share This Page