createSection throwing nullPointerException.

Discussion in 'Spigot Plugin Development' started by Aderm, May 31, 2016.

Thread Status:
Not open for further replies.
  1. 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
     
    #1 Aderm, May 31, 2016
    Last edited by a moderator: Aug 3, 2018
  2. 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
     
    • Agree Agree x 1
  3. 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?
     
  4. You use the "target" object without checking if it is null. Offline players will NPE that code.
     
    • Agree Agree x 1
  5. Was just about to post a new replay quoting the objects in the code. RIP your ninja skills.
     
  6. It's the offline player throwing npe.
    I need to be able to ban offline players though?
     
  7. Right, so it's the createSection that throws NPE.

    line 49 is: plugin.getConfig().createSection(offlineTarget.getUniqueId().toString());
     
  8. So just make all online players offline players. Don't do the whole Player target thing
     
  9. What..?
    how tf do I do that.
    Make all online players offline players makes no sense
     
  10. 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.
     
  11. No idea, should return their UUID if they've joined before.
     
  12. 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
     
  13. 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.
     
  14. How do you suggest I do that?
     
  15. @HTML like @Synapz said, why do you have an empty constructor? If you call that, of course plugin will be null
     

  16. Also for the null issue. Print out the plugin the config and the target to see which exactly is null
     
  17. idfk?
    It told me to add it, so I did?
    Idk what to put in it.
     
  18. Seriously you have to be kidding me lol.

    Don't just blindly follow tutorials. learn java
     
  19. This shit is annoying as fuck.
    I'm not following tutorials, and I plan to learn some more tomorrow.
     
  20. @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
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.