[SOLVED] A real headache - why onCommand is crashing my server?

Discussion in 'Plugin Development' started by zajacmp3, Aug 8, 2012.

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

    zajacmp3

    Hello,

    What already I have done:

    1) I removed all other plugin and left only mine.
    2) checked the code and isolated the part that is crashing it.

    I will try to make it brief and simple. I wrote a command listener - or in other way a command executor.
    Code:
            //getCommand("vote").setExecutor(votecmd);
    That is referring to:
    Code:
        private VoteCommand votecmd;
    That is referring to:

    Code:
    package com.zajacmp3.survivalgame;
    
    import java.awt.List;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class VoteCommand implements CommandExecutor {
        
        static SurvivalGame survivalgame = new SurvivalGame();
        static List votersList = new List();
    
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
            Player player = (Player) sender;
            if(cmdLabel.equalsIgnoreCase("vote") && SurvivalGame.hasVotingStarted){
                if(player.hasPermission("survivalgame.vote")){
                    int howManyMaps = SurvivalGame.config.getInt("MapConfiguration.HowManyMaps");
                    if(Integer.parseInt(args[0])<=howManyMaps && Integer.parseInt(args[0])>=howManyMaps && haveHeVoted(player)!=true){
                        votersList.add(player.getName());
                        SurvivalGame.voteMapList[1][Integer.parseInt(args[0])] = SurvivalGame.voteMapList[1][Integer.parseInt(args[0])+1];
                    }
                }
                else{
                    player.sendRawMessage("You do not have permission to do this");
                }
            }
            else{
                player.sendRawMessage("Voting for another map have not started yet");
            }
            return false;
        }
    
        private boolean haveHeVoted(Player player) {
            if(votersList.equals(player.getName())){
                return true;
            }
            else
                return false;
        }
    
    }
    

    Error on crash that I get is:
    Code:
    ---- Minecraft Crash Report ----
    // Oh - I know what I did wrong!
    
    Time: 8/8/12 10:51 AM
    Description: Exception in server tick loop
    
    java.lang.NullPointerException
        at org.bukkit.craftbukkit.help.SimpleHelpMap.initializeCommands(SimpleHelpMap.java:120)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:255)
        at net.minecraft.server.MinecraftServer.i(MinecraftServer.java:296)
        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:275)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:225)
        at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:140)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:380)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    
    Relevant Details:
    - Minecraft Version: 1.3.1
    - Operating System: Linux (amd64) version 2.6.18-238.12.1.el5.028stab091.1
    - Java Version: 1.6.0_24, Sun Microsystems Inc.
    - Java VM Version: OpenJDK 64-Bit Server VM (mixed mode), Sun Microsystems Inc.
    - Memory: 889009648 bytes (847 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB)
    - JVM Flags: 2 total; -Xmx1024M -Xms1024M
    - Type: Dedicated Server
    - Is Modded: Definitely; 'craftbukkit'
    - Profiler Position: N/A (disabled)
    - Player Count: 0 / 20; []
    - World Lobby Entities: 0 total; []
    - World Lobby Players: 0 total; []
    - World Lobby Chunk Stats: ServerChunkCache: 625 Drop: 0


    What I think I done wrong:

    1) Bad code writing in VoteCommand class that is making NullPointerExeption.

    What I need help with:

    1) locating my mistake
    2) help me find a way to get it working
     
  2. Offline

    Courier

    It sounds like votecmd is null. Post the code surrounding "//getCommand("vote").setExecutor(votecmd);"
     
  3. Offline

    zajacmp3

    Here you are. Everything there can be relevant surrounding.

    Code:
    public class SurvivalGame extends JavaPlugin implements Listener{
     
        static Counters counters = new Counters();
        static SurvialListeners survivallisteners = new SurvialListeners();
        static SurvivalGame plugin;
     
        //static things
        static int prebattlelenght = 5;
        static int battlelenght = 5;
        static int minimumnumberofplayers = 2;
        static int tempcountdown = 0;
        static int countdownTask = 0;
        static int blockMovement = 0;
        static boolean haveBattleStarted = false;
        static boolean hasVotingStarted = false;
        static int[][] voteMapList = new int[1][];
        static int nextMap = 1;
        //List of Players that started Battle
        static List<String> listOfQualifiedPlayers = new ArrayList<String>();
        static FileConfiguration config = null;
     
        public void onDisable(){
            this.getServer().getScheduler().cancelAllTasks();
        }
        public void onEnable(){
            System.out.println("Test1");
            getCommand("vote").setExecutor(votecmd);
            System.out.println("Test2");
     
            plugin = this;
            config = this.getConfig();
            loadConfig();
            prepareBattle();
        }
    [...] Nothing relevant to commands after this
    When I was writing this command and all I was guided by a tutorial on how to write commands
    Source: http://forums.bukkit.org/threads/prefixed-commands-tutorial-organization.61890/

    @EDIT:

    I really don't have a clue how to properly write commands yet. I though that I made everything right going by the tutorial. If I won't be able to solve this I will have to go through hundreds lines of code on git hub of other developers to get their idea. And I really hope to avoid it...

    Okay, I have to double post...

    I solved it.

    How I did it? I was patiently reading Plugin Development and helping where I could waiting for an answer. And it came in another topic written by bitfreeze - Thanks.

    So I changed my code:
    Code:
    getCommand("vote").setExecutor(votecmd);
    to:

    Code:
    getCommand("vote").setExecutor(new VoteCommand());
    And I get different error:

    Code:
    Caused by: java.awt.HeadlessException:
    No X11 DISPLAY variable was set, but this program performed an operation which requires it.
            at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:173)
            at java.awt.List.<init>(List.java:233)
            at java.awt.List.<init>(List.java:188)
            at com.zajacmp3.survivalgame.VoteCommand.<clinit>(VoteCommand.java:13)
            ... 17 more
    
    So I changed in 13 line of VoteCommand class:
    From
    Code:
    static List votersList = new List();
    to:

    Code:
    static ArrayList<String> votersList = new ArrayList<String>();
    and it is working.

    Thanks everybody for even slightest interest in this topic.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
Thread Status:
Not open for further replies.

Share This Page