Solved NullPointer?!

Discussion in 'Plugin Development' started by bigflori, Dec 6, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    bigflori

    Hello!
    I ran into a problem, something went wrong here but I can't fix it! Please help me what's the problem.

    The error
    Code:
    [20:15:23 INFO]: BigFlori issued server command: /lotto
    [20:15:23 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'lotto' in plugin Lottery v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot112.jar:git-Spigot-625bc00-f4822eb]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot112.jar:git-Spigot-625bc00-f4822eb]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1353) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1188) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:?]
            at java.base/java.util.concurrent.FutureTask.run(Unknown Source) [?:?]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot112.jar:git-Spigot-625bc00-f4822eb]
            at java.base/java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.NullPointerException
            at me.bigflori.lottery.utils.LotteryManager.startTimer(LotteryManager.java:86) ~[?:?]
            at me.bigflori.lottery.commands.CommandLotto.onCommand(CommandLotto.java:20) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot112.jar:git-Spigot-625bc00-f4822eb]
            ... 15 more
    
    LotteryManager.java
    Code:
        private Core plugin;
        public LotteryManager(Core pl)
        {
            plugin = pl;
        }
    
        public int seconds = 10;
        int countdown;
        public boolean run_countdown = false;
        public void startTimer()
        {
            run_countdown = true;
            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                public void run()
                {
                    Bukkit.getServer().broadcastMessage("test");
                    run_countdown = false;
                }
               
            }, 20);
    
    CommandLotto.java
    Code:
    private Core plugin;
        public CommandLotto(Core pl)
        {
            plugin = pl;
        }
        LotteryManager lm = new LotteryManager(plugin);
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
            lm.startTimer();
            return false;
        }
    
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    MightyOne

    Code:
    LotteryManager.java:86
    little hard to say where that line is. use paste bin
     
  4. Perhaps you should take a look at the Plugin Tutorial, which goes into adding your own commands: https://bukkit.gamepedia.com/Plugin_Tutorial

    You'll need to check if cmd is "lotto", otherwise this will be done whenever any commands that your plugin has are run. Also, you'll need to have this class implement CommandExecutor, then add it in the main class to the server. You should look a bit into that as it is a bit complicated (I'm also happy to provide an example). Returning false for onCommand basically makes the command do the red "error exception" thing in the chat. Returning true means the command worked with no serious errors.
     
  5. The OP hasn't posted their full class so we have no way of knowing whether they added CommandExecutor or not.

    But OP:

    He is right, you should check if the cmd is equal to a certain type, then do whatever because that lets you manipulate your code just a little bit more. And return true like @octoberBkkitPlgins said
     
  6. Offline

    bigflori

  7. Offline

    Caderape2

    @bigflori
    Create it in your constructor and see if you still have the error. The variable plugin is maybe not initialized yet.
     
  8. Offline

    bigflori

    @Caderape2
    It only shows me this error when I use startTimer() method with command.
    When I execute it from the main class it works.

    EDIT: By the way, it's still not working :(
     
    Last edited: Dec 7, 2017
  9. Why do you want there to be more than one LotteryManager at a given time? Are you wishing to start the lottery each time the command occurs? You could use LotteryManager in a static way, and also create a static method initialize(Core core) {//do stuff}. I very often use static managers, or you can create the manager in the main class, and only have that one manager on enabling (you can either re-use that reference from the main class or create new references in constructors).
     
  10. Online

    timtower Administrator Administrator Moderator

    Those static managers have a tendency to get misused though, calling the init method multiple times, no proper singleton, people forget to initialize them a lot.
     
  11. This is the exact reason why I had to stop one of my projects mid project, to rewrite all the managers to only be initialized in the main class. It makes your code cleaner, more efficient, and less prone to errors and bugs
     
  12. Offline

    bigflori

    Thank you guys for help!
    I solved the problem. :)
    I just put the methods to main class and it works!
     
Thread Status:
Not open for further replies.

Share This Page