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 (Move your mouse to reveal the content) Dont work (open) Dont work (close) 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!
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.
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;
@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;'.
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.
@Qearlot Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 Called it. Do a length check. And that was stripped btw.
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.