Null error

Discussion in 'Plugin Development' started by Qearlot, May 15, 2018.

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

    Qearlot

    Hey guys! I need help at my plugin! Now I make plugin which sets my game mode on command.
    But when I just write [/gm] then console write Error: Null.
    How can I fix this?
    Dont work (open)
    GameMode gm; int gm;

    Code:
    package server.main;
    
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandSetGamemode implements CommandExecutor {
    
       
        public CommandSetGamemode(Server plugin) {
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String str, String[] args) {
            Player p = (Player) sender;
            String selectNumber = args[0];
            GameMode gm = null;       
            switch (selectNumber) {
                case "0": gm = GameMode.SURVIVAL;
                break;
                case "1": gm = GameMode.CREATIVE;
                break;
                case "2": gm = GameMode.ADVENTURE;
                break;
                case "3": gm = GameMode.SPECTATOR;
                break;
                default: 
                p.sendMessage("§f§lВыберите правильный режим.");
               
                p.sendMessage("§a|Список режимов|");
               
                p.sendMessage("§f§lВыживание - §a0");
               
                p.sendMessage("§f§lКреатив - §a1");
               
                p.sendMessage("§f§lПриключение - §a2");
               
                p.sendMessage("§f§lНаблюдатель - §a3");
               
                p.sendMessage("§f§lИспользование - §a[/gm <режим>]");
                break;
            }
            p.setGameMode(gm);
    
            return true;
        }
    
    }
    
    Thanks who had tried help me!
     
  2. Offline

    timtower Administrator Administrator Moderator

    Changed title to something more descriptive.
    @Qearlot Where does it print? And please format your code, it is not easy to read right now.
    Don't cast to Player without checking if the sender is one.
     
  3. args[0] = null if you don't put arguments after the command /gm.

    Example: /gm first second third
    args[0] = first
    args[1] = second
    args[2] = third

    So if you told you us you only wrote '/gm' that means that args[0] doesn't exists because the argument length is zero.
    To fix this you must put an if (args.length == 0) return false;
     
  4. Offline

    timtower Administrator Administrator Moderator

    IndexOutOfBoundsException, not null.
     
  5. @timtower
    Thanks for correcting me, I did not see the first line.
    It is true because otherwise the error would be that one, so the only cause is '
    Player p = (Player) sender;'.
     
  6. Offline

    timtower Administrator Administrator Moderator

    That would be a ClassCastException if it goes wrong, till then I expect that p.setGameMode(null); is being called.
    But we can't know that for sure without the full error.

    @Qearlot Please post your full server log using http://pastebin.com without stripping anything away.
     
  7. Offline

    Qearlot

  8. Offline

    timtower Administrator Administrator Moderator

    @Qearlot Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    Called it.
    Do a length check.

    And that was stripped btw.
     
  9. Offline

    Qearlot

    args.length ?
     
  10. Offline

    timtower Administrator Administrator Moderator

    Yes, as
    posted.
     
  11. Offline

    Qearlot

    dont work(

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String str, String[] args) {
            Player p = (Player) sender;
            String selectNumber = args[0];
            if (args.length == 0) {
                return false;
            }
               
           
            GameMode gm = null;       
            switch (selectNumber) {
                case "0": gm = GameMode.SURVIVAL;
                break;
                case "1": gm = GameMode.CREATIVE;
                break;
                case "2": gm = GameMode.ADVENTURE;
                break;
                case "3": gm = GameMode.SPECTATOR;
                break;
                default: 
                p.sendMessage("§f§lВыберите правильный режим.");
               
                p.sendMessage("§a|Список режимов|");
               
                p.sendMessage("§f§lВыживание - §a0");
               
                p.sendMessage("§f§lКреатив - §a1");
               
                p.sendMessage("§f§lПриключение - §a2");
               
                p.sendMessage("§f§lНаблюдатель - §a3");
               
                p.sendMessage("§f§lИспользование - §a[/gm <режим>]");
                break;
            }
            p.setGameMode(gm);
    
            return true;
        }
    https://pastebin.com/ddkZYKft

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 15, 2018
  12. Offline

    timtower Administrator Administrator Moderator

    @Qearlot Check the length BEFORE you use args[0]
     
Thread Status:
Not open for further replies.

Share This Page