hey guys, i guess this is my third thread, today. Really Amazing . Let's look at what i done! Here is SignAutoUpdaterListener: Code: package ir.envica.main.sign; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; import ir.envica.main.main; import net.md_5.bungee.api.ChatColor; public class SignAutoUpdater implements Listener { private static SignAutoUpdater instance; public static SignAutoUpdater getInstance() { return instance; } private Location signLocation; public Location getsignLocation() { return signLocation; } public void setsignLocation(Location signLocation) { this.signLocation = signLocation; } @EventHandler public void signPlaced(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("[bl]")) { event.setLine(0, "§6[§5BattleLeague§6]"); event.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); event.setLine(2, "§3click here to join"); setsignLocation(event.getBlock().getLocation()); } } @EventHandler public void signUpdater(Location signLocation) { World w = signLocation.getWorld(); Block b = w.getBlockAt(signLocation); if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN || b.getState() instanceof Sign) { Sign sign = (Sign) b.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } } @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { if (e.getClickedBlock().getState() instanceof Sign) { Sign sign = (Sign) e.getClickedBlock().getState(); if (sign.getLine(0).equalsIgnoreCase("§6[§5BattleLeague§6]")) { main.getInstance().checkLobbyAndSend(e.getPlayer()); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } } } } } And this is a part of CommandExecutor: Code: // Leave if (args[0].equalsIgnoreCase("leave")) { // Leave the lobby main.getInstance().checkLobbyAndLeave(p); // Change the Sign int SignAutoUpdater sign = SignAutoUpdater.getInstance(); Location signLocation = sign.getsignLocation(); sign.signUpdater(signLocation); } Also this is my crash log : Code: [23:04:27] [Server thread/INFO]: Colevin issued server command: /bl leave [23:04:27] [Server thread/ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'bl' in plugin BattleLeague v0.5.5 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_25] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) [?:1.7.0_25] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_25] at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at java.lang.Thread.run(Unknown Source) [?:1.7.0_25] Caused by: java.lang.NullPointerException at ir.envica.main.commands.CommandBattleleague.onCommand(CommandBattleleague.java:149) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] ... 16 more [23:04:37] [Server thread/INFO]: Colevin lost connection: Disconnected [23:04:37] [Server thread/INFO]: Colevin left the game. What's wrong? Why location is not reachable here? Code: Caused by: java.lang.NullPointerException
yes, why not? Code: Location signLocation = sign.getsignLocation(); Yes, I know! but, why signLocation isn't null in SignAutoUpdaterListener, but in CommandExecutor is null?
Sign is null. You imported bungee chat color instead of bukkit one. Check is sign null before doing anything else.
This sign? Code: SignAutoUpdater sign = SignAutoUpdater.getInstance(); no, this sign is not null! In Line 149: Code: Location signLocation = sign.getsignLocation(); signLocation is null yeah? here a test i done: in SignAutoUpdaterListener. Sign location saved in SignLocation.Place Code: main.getInstance().getConfig().set("SignLocation.Place", signLocation); main.getInstance().saveConfig(); But the same example on CommandExecutor didn't work! Code: // Change the Sign int SignAutoUpdater sign = SignAutoUpdater.getInstance(); main Main = main.getInstance(); Location signLocation = sign.getsignLocation(); Main.getConfig().set("SignLocation.Command", signLocation); Main.saveConfig(); sign.signUpdater(signLocation); There is no error log in eclipse! where's the problem? why i code can not get location in CommandExecutor? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Sign is null. End of. <lession> Do you know how does an NPE work? NPE triggers when you try to execute a method on a null object. If line 149 is Code: Location signLocation = sign.getsignLocation(); Location is not ConfigurationSerializable. Again, you imported BungeeCord chatcolor instead of Bukkit chatcolor. It is null because you never initiate instance that getInstance is returning in the first place. NullPointerException in the docs clearly states: If you make a getInstance a static method with a static field instance that gets returned, make sure you initialize the instance. </lession> <solution> Make a constructor myClass() and in it put Code: . . . myStaticInstanceField = this. . . . </solution>
When and where do you assign a value to the instance? Never, thats what causes the NPE In the getInstance() method you need to first check if the instance is there, so Code: if (instance == null) instance = new SignAutoUpdater(); return instance; Or if you want it to be even cooler in 1 line: Code: return instance == null ? instance = new SignAutoUpdater() : instance; When you call getInstance() the first time, it creates a new instance of the class and returns it, when you call the method another time it just returns the instance
Crash Log: Code: [19:50:41] [Server thread/ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'bl' in plugin BattleLeague v0.5.5 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_25] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) [?:1.7.0_25] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_25] at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.9.jar:git-Spigot-7d15d07-c194444] at java.lang.Thread.run(Unknown Source) [?:1.7.0_25] Caused by: java.lang.NullPointerException at ir.envica.main.sign.SignAutoUpdater.signUpdater(SignAutoUpdater.java:46) ~[?:?] at ir.envica.main.commands.CommandBattleleague.onCommand(CommandBattleleague.java:150) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444] ... 16 more Code: SignAutoUpdater sign = SignAutoUpdater.getInstance(); // Line 148 Location signLocation = sign.getsignLocation(); // Line 149 sign.signUpdater(signLocation); // Line 150 Code: public void signUpdater(Location signLocation) // Line 46{ World w = signLocation.getWorld(); Block b = w.getBlockAt(signLocation); if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN || b.getState() instanceof Sign) { Sign sign = (Sign) b.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } else { main.getInstance().getLogger().info(ChatColor.RED + "No Sign Set!"); } }
World w = signLocation.getWorld(); signLocation is null. sign.getsignLocation(); returns null. You do not init the signLocation for the first time so it will be handled as null.
Yes you are right! Code: package ir.envica.main.sign; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; import ir.envica.main.main; public class SignAutoUpdater implements Listener { private static SignAutoUpdater instance; public static SignAutoUpdater getInstance() { return instance == null ? instance = new SignAutoUpdater() : instance; } private Location signLocation; public Location getsignLocation() { return signLocation; } public void setsignLocation(Location signLocation) { this.signLocation = signLocation; } @EventHandler public void signPlaced(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("[bl]")) { event.setLine(0, "§6[§5BattleLeague§6]"); event.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); event.setLine(2, "§3click here to join"); setsignLocation(event.getBlock().getLocation()); } } public void signUpdater(Location signLocation) { this.signLocation = signLocation; Location Locatedsign = (Location) getsignLocation(); ConsoleCommandSender msg = main.getInstance().getServer().getConsoleSender(); if (signLocation != null) { msg.sendMessage(ChatColor.GREEN + "sign Location is not Null! (NPE) solved!"); World w = signLocation.getWorld(); Block b = w.getBlockAt(signLocation); if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN || b.getState() instanceof Sign) { Sign sign = (Sign) b.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } else { main.getInstance().getLogger().info(ChatColor.RED + "No Sign Set!"); } } else { msg.sendMessage(ChatColor.RED + "Sorry,SignLocation is null! NPE"); msg.sendMessage(ChatColor.YELLOW + "Trying to getSignLocation in an otherway!"); if (getsignLocation() != null) { msg.sendMessage(ChatColor.GREEN + "Started!"); World world = Locatedsign.getWorld(); msg.sendMessage(ChatColor.GREEN + "World Location is in our hands!"); Block block = world.getBlockAt(signLocation); msg.sendMessage(ChatColor.GREEN + "Block Location is in our hands!"); if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN || block.getState() instanceof Sign) { Sign sign = (Sign) block.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } else { msg.sendMessage(ChatColor.RED + "There is no sign in saved loaction!"); } } else { msg.sendMessage(ChatColor.GREEN + "We tried hard! but there is an error here! (NPE)"); } } } @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { if (e.getClickedBlock().getState() instanceof Sign) { Sign sign = (Sign) e.getClickedBlock().getState(); if (sign.getLine(0).equalsIgnoreCase("§6[§5BattleLeague§6]")) { main.getInstance().checkLobbyAndSend(e.getPlayer()); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } } } } } signLocation is null, But how can i fix it? could you give me more hint I'm new in Java.
@envic Just set the location before using it. I dont know how you determain it, but first thing you should do is set it.
@ArsenArsen :\ but it already set! here: Code: @EventHandler public void signPlaced(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("[bl]")) { event.setLine(0, "§6[§5BattleLeague§6]"); event.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); event.setLine(2, "§3click here to join"); setsignLocation(event.getBlock().getLocation()); // Here } } After export!, reload the server!placing a sign!& join to lobby,then by typing /bl leave! NPE! if i get the config file & set a path to sign location, i'll have the signlocation in config file! With such this Listener.java: Code: package ir.envica.main.sign; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; import ir.envica.main.main; public class SignAutoUpdater implements Listener { private static SignAutoUpdater instance; public static SignAutoUpdater getInstance() { return instance == null ? instance = new SignAutoUpdater() : instance; } private Location signLocation; public Location getsignLocation() { return signLocation; } public void setsignLocation(Location signLocation) { this.signLocation = signLocation; } main getmain = main.getInstance(); @EventHandler public void signPlaced(SignChangeEvent event) { if (event.getLine(0).equalsIgnoreCase("[bl]")) { event.setLine(0, "§6[§5BattleLeague§6]"); event.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); event.setLine(2, "§3click here to join"); setsignLocation(event.getBlock().getLocation()); } } public void signUpdater(Location signLocation) { this.signLocation = signLocation; Location Locatedsign = (Location) getsignLocation(); getmain.getConfig().set("Test.OnsignUpdater.Sign.Location", signLocation); getmain.saveConfig(); ConsoleCommandSender msg = main.getInstance().getServer().getConsoleSender(); if (signLocation != null) { msg.sendMessage(ChatColor.GREEN + "sign Location is not Null! (NPE) solved!"); World w = signLocation.getWorld(); Block b = w.getBlockAt(signLocation); if (b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN || b.getState() instanceof Sign) { Sign sign = (Sign) b.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } else { main.getInstance().getLogger().info(ChatColor.RED + "No Sign Set!"); } } else { msg.sendMessage(ChatColor.RED + "Sorry,SignLocation is null! NPE"); msg.sendMessage(ChatColor.YELLOW + "Trying to getSignLocation in an otherway!"); if (getsignLocation() != null) { msg.sendMessage(ChatColor.GREEN + "Started!"); World world = Locatedsign.getWorld(); msg.sendMessage(ChatColor.GREEN + "World Location is in our hands!"); Block block = world.getBlockAt(signLocation); msg.sendMessage(ChatColor.GREEN + "Block Location is in our hands!"); if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN || block.getState() instanceof Sign) { Sign sign = (Sign) block.getState(); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); } else { msg.sendMessage(ChatColor.RED + "I we see, there is no sign in saved loaction!"); } } else { msg.sendMessage(ChatColor.GREEN + "We tried hard! but there is an error here! (NPE)"); } } } @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { if (e.getClickedBlock().getState() instanceof Sign) { Sign sign = (Sign) e.getClickedBlock().getState(); if (sign.getLine(0).equalsIgnoreCase("§6[§5BattleLeague§6]")) { main.getInstance().checkLobbyAndSend(e.getPlayer()); sign.setLine(1, "§a<" + ChatColor.AQUA + main.getInstance().pInLobbyCount() + "§a/4>"); sign.update(); getmain.getConfig().set("Test.OnClick.Sign.Location", signLocation); getmain.saveConfig(); } } } } } i'll have such this config.yml: Code: Test: OnClick: Sign: Location: ==: org.bukkit.Location yaw: 0.0 pitch: 0.0 z: 260.0 y: 62.0 world: world x: 171.0 EDIT by Moderator: merged posts, please use the edit button instead of double posting.
I just realized.. It's a listener.. Bukkit makes one allready. Make a constructor public Myclass() { inst = this }
don't you think, it have to work with this.signLocation = signLocation; ? I'm confused, How can i make a constructor for signLocation but keep my signLocation on private Location signLocation;? If you say make a constructor and add signLocation = this ! I have to change my private Location signLocation to private SignAutoUpdater signLocation;
No, you make a constructor to your class which takes no arguments. That will get called when you register your listener. Then in it you set your instance to 'this' So public MyClass() { instance = this; } Please read about constructors. It's basic java.
Oh, I read init instead of instance, Sorry Yes i done it before! for main class! but when i read it init i really confused,
I hope you'll Close your eyes to my mistakes I know how noobi are my mistakes & questions Thanks for your help! let me test the plugin! Yes, my mistake solved! special thanks Arsen & FisheyLP EDIT by Moderator: merged posts, please use the edit button instead of double posting.