NullPointerException

Discussion in 'Plugin Development' started by DREAM_MACHINE, Mar 18, 2013.

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

    DREAM_MACHINE

    I can't figure out why this is happening. Every time I do /bq add [SOMETHING] It gives me this error:

    org.bukkit.command.CommandException: Unhandled exception executing command 'bq' in plugin BasicQuiz v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.v1_5_R1.CraftServer.dispatchCommand(CraftServer.java:514)
    at net.minecraft.server.v1_5_R1.PlayerConnection.handleCommand(PlayerConnection.java:967)
    at net.minecraft.server.v1_5_R1.PlayerConnection.chat(PlayerConnection.java:885)
    at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java:840)
    at net.minecraft.server.v1_5_R1.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.v1_5_R1.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R1.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at me.m277772.BQuiz.BQuizMain.onCommand(BQuizMain.java:56)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 15 more

    Heres my code:
    Code:
    package me.m277772.BQuiz;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BQuizMain extends JavaPlugin {
       
        public static BQuizMain plugin;
        public final Logger logger = Logger.getLogger("BQuiz");
        public final String[] quizName = new String[100];
        public final String[] quizQuestion1 = new String[30];
        int quizCounter = 0;
       
       
        @Override
        public void onDisable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has been disabled!");
        }
       
        @Override
        public void onEnable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has been enabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            boolean isNumber;
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("bq") || commandLabel.equalsIgnoreCase("basicquiz"))
            {
                if(args.length == 0)
                {
                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "Test.");
                }
                if(args.length > 0)
                {
                    if(args[0].equalsIgnoreCase("add"))
                    {
                        if(args.length > 1 && quizCounter < 101 && args.length < 3)
                        {
                            for(int i = 0; quizName.length > i; i++)
                            {
                                String QuizNames = quizName[i];
                                if(args.length > 1)
                                {
                                    if(QuizNames.equalsIgnoreCase(args[1]))
                                    {
                                   
                                    }else
                                    {
                                        quizName[quizCounter] = args[1];
                                        quizCounter++;
                                        player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "A quiz named: " + ChatColor.ITALIC + args[1] + ChatColor.GREEN + "has been added.");
                                    }
                                }
                            }
                        }
                    }
                    if(args[0].equalsIgnoreCase("sq") || args[0].equalsIgnoreCase("setquestion"))
                    {
                        if(args.length > 1)
                        {
                            isNumber = true;
                            try
                            {
                                Integer.parseInt(args[1]);
                            }
                            catch(NumberFormatException e)
                            {
                                isNumber = false;
                            }
                                if(args.length > 2 && isNumber == true)
                                {
                                player.sendMessage(":D");
                                }
                        }
                    }
                }
            }
           
            return false;
        }
    }
    
    Please help if possible.
     
  2. http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/
    You should learn to read the errors and follow the stack trace :p

    The problem is at your line 56:
    Code:
    for(int i = 0; quizName.length > i; i++)
    {
        String QuizNames = quizName[i];
        if(args.length > 1)
        {
           if(QuizNames.equalsIgnoreCase(args[1])) // <<< here
           {
    It's because the QuizNames variable (which is badly named by the way, you should not use variables/fields/methods starting with upper case letter) is most likely null and you're using a method on it, and basically translates to "null.equalsIgnoreCase(...)" which just can't happen.

    You should also learn about Lists, they have dynamic size unlike arrays which have fixed size.
     
    DREAM_MACHINE likes this.
  3. Offline

    DREAM_MACHINE

    Thanks for helping me find my error. Also thanks for giving me that tutorial link, it really helped!
     
  4. Offline

    kreashenz

    DREAM_MACHINE I actually think its either the args[1] or the QuizNames.
     
Thread Status:
Not open for further replies.

Share This Page