Hey guys, I'm trying to code a bans plugin, and I'm storing everything in the config. Don't tell me to use MY-SQL, because I am not going to do that. This is my whole class, feel free to use it if you want? Code (Java): package me.adam.core.commands; import java.util.Date; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerLoginEvent; import me.adam.core.Main; public class Bans implements Listener, CommandExecutor { Main plugin; public Bans(Main instance) { plugin = instance; } public Bans() {} Date date = new Date(); String banReason = "§cBreaking the rules or other."; @Override public boolean onCommand(CommandSender s, Command c, String arg2, String[] args) { if (c.getName().equalsIgnoreCase("ban") && (args.length == 0) && s.hasPermission("ban.use")) { s.sendMessage("§cPlease specify a player to ban"); s.sendMessage("§cSyntax: /ban <player> <reason>"); return true; } if (args.length == 1) { @SuppressWarnings("deprecation") OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(args[0]); Player target = Bukkit.getPlayer(args[0]); s.sendMessage("§3" + target.getName() + " §awas banned for: " + banReason); target.kickPlayer("§4§lYou've been banned!"); plugin.getConfig().createSection(target.getUniqueId().toString()); plugin.getConfig().set(target.getUniqueId().toString() + ".whoBanned", s.getName()); plugin.getConfig().set(target.getUniqueId().toString() + ".dateBanned", date); plugin.getConfig().set(target.getUniqueId().toString() + ".reason", banReason); plugin.getConfig().createSection(offlineTarget.getUniqueId().toString()); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".whoBanned", s.getName()); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".dateBanned", date); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".reasonForBan", banReason); plugin.saveConfig(); return true; } if (args.length >= 2) { @SuppressWarnings("deprecation") OfflinePlayer offlineTarget = Bukkit.getOfflinePlayer(args[0]); Player target = Bukkit.getPlayer(args[0]); StringBuilder sb = new StringBuilder(); for (int i = 1; i < args.length; i++) { sb.append(args[i]).append(" "); } String banReason = ChatColor.translateAlternateColorCodes('&', sb.toString()); target.kickPlayer("§4§lYou've been banned!"); plugin.getConfig().createSection(target.getUniqueId().toString()); plugin.getConfig().set(target.getUniqueId().toString() + ".whoBanned", s.getName()); plugin.getConfig().set(target.getUniqueId().toString() + ".dateBanned", date); plugin.getConfig().set(target.getUniqueId().toString() + ".reasonForBan", banReason); plugin.getConfig().createSection(offlineTarget.getUniqueId().toString()); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".whoBanned", s.getName()); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".dateBanned", date); plugin.getConfig().set(offlineTarget.getUniqueId().toString() + ".reasonForBan", banReason); plugin.saveConfig(); return true; } if(!(s.hasPermission("ban.use"))) { s.sendMessage("§cYou cannot do that!"); return true; } return false; } @EventHandler public void onJoinChecker(PlayerLoginEvent e) { Player p = e.getPlayer(); if(plugin.getConfig().contains(p.getUniqueId().toString())) { e.setKickMessage("§cYou have been banned!\n§3You were banned by: §a" + plugin.getConfig().getString(p.getUniqueId().toString() + ".whoBanned") + "\n §cDate you were banned: §a" + plugin.getConfig().getString(p.getUniqueId().toString() + ".dateBanned") + "\n§cYou were banned for: §r" + plugin.getConfig().getString(p.getUniqueId().toString() + ".reasonForBan") + "\n§8§l§m--------------------------------------------- \n§3Unless stated otherwise, feel free to appeal @ website.com!"); } else { e.allow(); } } } And my stacktrace: Code (Text): [19:15:38 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'core:ban' in plugin Core v1 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91] at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24] at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91] Caused by: java.lang.NullPointerException at me.adam.core.commands.Bans.onCommand(Bans.java:49) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24] ... 15 more It basically throws a nullPointerException whenever I try to write something to config. Is there a better way to store UUID's for this type of thing? Thanks
Hmmmm Here's an idea. Maybe your config reference is null. Or better yet, maybe your plugin variable is null. It sure is possible seeing how you (for some strange reason) did public Ban() to show an explicit constructor(don't know why). Remove the Ban() constructor and make it Private so your plugin variable will always be set and not throw null pointers
I feel like it would be because he is trying to retrieve a value from a null object in the code. @HTML since there aren't line numbers in the browser, which line is specifically 49?
Right, so it's the createSection that throws NPE. line 49 is: plugin.getConfig().createSection(offlineTarget.getUniqueId().toString());
Out of curiosity, see if #getUniqueId returns null for the offline player. As far as I'm aware it should return a random UUID but perhaps there was an NMS change which makes it return null for unknowns now.
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); If you do that for a Player object and they are offline it will return null. So just keep them all as offline players
Not sure if it still works but here is a resource called UUIDFetcher. You can search for the names and it fetches them from the interwebs.
@HTML like @Synapz said, why do you have an empty constructor? If you call that, of course plugin will be null
@HTML If you have a constructor that doesn't initialize a field, it will be null. remove the empty constructor and pass the main instance on the places you use the empty constructor