Hi! I am trying to typecast like this: PHP: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if(commandLabel.equalsIgnoreCase("setspawn")) { setSpawn((Player)sender); //fails here - line 44 return true; } return false;} But it already seems to fail at calling the setSpawn function. Error: Damn, I don't know why it is getting this error, but in all my other plugins typecasting just worked without any problems.
You're only giving a portion of the code, and a portion of the error. Post what the setSpawn function does, and post the entire error, not just one line.
It is not the type cast that is failing - that throws a different exception, I think it is ClassCastException, not a NullPointerException.
Gah, I hate setting code free before it isn't released yet but there aren't any other options. PHP: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if(commandLabel.equalsIgnoreCase("setspawn")) { setSpawn((Player)sender); return true; } else if(commandLabel.equalsIgnoreCase("spawn")) { if(spawnLocation != null) { ((Player)sender).teleportTo(spawnLocation); sender.sendMessage(ChatColor.GREEN + "Successfully teleported to spawn"); } else { sender.sendMessage(ChatColor.RED + "No spawn has been set yet!"); } return true; } return false; } public void setSpawn(Player player) { Settings.setDouble("spawnLocation.x", player.getLocation().getX()); Settings.setDouble("spawnLocation.y", player.getLocation().getY()); Settings.setDouble("spawnLocation.z", player.getLocation().getZ()); Settings.setString("spawnLocation.yaw", Float.toString(Float.valueOf(player.getLocation().getYaw()))); Settings.setString("spawnLocation.pitch",Float.toString(Float.valueOf(player.getLocation().getPitch())); Settings.setString("spawnLocation.world", player.getWorld().getName()); updateSpawnLocation(); player.sendMessage(ChatColor.GREEN + "Successfully set spawn"); } public void updateSpawnLocation() { double x = Settings.getDouble("spawnLocation.x"); double y = Settings.getDouble("spawnLocation.y"); double z = Settings.getDouble("spawnLocation.z"); float yaw = Float.valueOf(Settings.getString("spawnLocation.yaw").trim()).floatValue(); float pitch = Float.valueOf(Settings.getString("spawnLocation.pitch").trim()).floatValue(); World world = getServer().getWorld(Settings.getString("spawnLocation.world")); spawnLocation = new Location(world, x, y, z, yaw, pitch); } There it is, below the error. error (Move your mouse to reveal the content) error (open) error (close) 2011-03-17 21:06:13 [INFO] Infernus666 [/5.150.254.91:50593] logged in with entity id 147 2011-03-17 21:06:25 [SEVERE] null org.bukkit.command.CommandException: Unhandled exception executing command 'setspawn' in plugin SpawnPlugin v1.0 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:37) at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:80) at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:183) at net.minecraft.server.NetServerHandler.c(NetServerHandler.java:645) at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:608) at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:602) at net.minecraft.server.Packet3Chat.a(SourceFile:24) at net.minecraft.server.NetworkManager.a(SourceFile:230) at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:76) at net.minecraft.server.NetworkListenThread.a(SourceFile:100) at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:357) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:272) at net.minecraft.server.ThreadServerApplication.run(SourceFile:366) Caused by: java.lang.NullPointerException at me.Infernus.SpawnPlugin.SpawnPlugin.setSpawn(SpawnPlugin.java:61) at me.Infernus.SpawnPlugin.SpawnPlugin.onCommand(SpawnPlugin.java:44) at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35) ... 12 more 2011-03-17 21:07:02 [WARNING] Failed to handle packet: java.lang.NullPointerException
It wasn't that neccesary for testing as a player myself. I can change it when I am going to release this.
Is line 61 this? Code: Settings.setString("spawnLocation.yaw", Float.toString(Float.valueOf(player.getLocation().getYaw()))); If so, maybe tease all that nesting and see which bit is not working? i.e. try it with a hardcoded string there first, then see which bit of that statement with the Float to string is not correct
I'm not positive but I'd assume these two lines: Code: Settings.setString("spawnLocation.yaw", Float.toString(Float.valueOf(player.getLocation().getYaw()))); Settings.setString("spawnLocation.pitch",Float.toString(Float.valueOf(player.getLocation().getPitch())); are going to cause issues. getYaw/getPitch both return floats, so it should be: Code: Settings.setString("spawnLocation.yaw", Float.toString(player.getLocation().getYaw())); Settings.setString("spawnLocation.pitch", Float.toString(player.getLocation().getPitch())); I don't know if that's where the NPE is, as we lack any way to tell what the line numbers are.
You can find my whole project here, I guess that'll be much easier easier. I'll try to find out why it throws me an error aswell meanwhile.
See, that's WAY more helpful. Your NPE is that you never initialize "Settings" So what you'll want to do is in onEnable() add something to the effect of: Settings = new SpawnPluginSettings(this.getDataFolder() + "/" + "settings.cfg"); (I use a "/" because it works in both Unix and Windows without issue, I've had issue with File.separator) I'd also recommend in SpawnPluginSetting.java that you add: file.getParentFile().mkdirs(); before you call file.save() in the constructor, just so that the directories for the config file exist before trying to create the file, it fails otherwise.