Solved I can't fix this NPE

Discussion in 'Spigot Plugin Development' started by _xXProDudeXx_, Apr 17, 2017.

  1. Hello Spigotiers,

    As the title already says, there is a NullPointerException which I can't fix.

    Here is the code I am using the methods:

    Code (Text):
    if (!WorldManager.getInstance().hasTitle(Bukkit.getWorld(args[0]))) {
                            p.sendMessage("┬žcThis world doesn't have a title!");
                            return true;
                        } else {
                            p.sendMessage("┬žaSuccesfully removed a title!");
    Here are the methods:

    Code (Text):
    public TitleObject getTitle(World w) {
            FileConfiguration c = WorldTitles.getInstance().getWorldFile().getConfig();
            ConfigurationSection configSection = c.getConfigurationSection("Worlds");
            if (configSection.getConfigurationSection(w.getName()) == null) {
                return null;
            } else {
                ConfigurationSection cs = configSection.getConfigurationSection(w.getName());
                String title = cs.getString("Title");
                String subtitle = cs.getString("Subtitle");
                int fadeIn = cs.getInt("FadeIn");
                int stay = cs.getInt("Stay");
                int fadeOut = cs.getInt("FadeOut");
                return new TitleObject(w, title, subtitle, fadeIn, stay, fadeOut);

        public boolean hasTitle(World w) {
            return this.getTitle(w) != null;
    In this case, the first 'if-statement' in the getTitle(World w) method gives me an NullPointerException as this is the stacktrace:

    Code (Text):
    [11:20:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'deltitle' in plugin WorldTitles v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute( ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_121]
            at Source) [?:1.8.0_121]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at brutalheroes._xxprodudexx_.worldtitles.manage.WorldManager.getTitle( ~[?:?]
            at brutalheroes._xxprodudexx_.worldtitles.manage.WorldManager.hasTitle( ~[?:?]
            at brutalheroes._xxprodudexx_.worldtitles.cmd.DelTitle.onCommand( ~[?:?]
            at org.bukkit.command.PluginCommand.execute( ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more

    Maybe I am just being dumb and not seeing the error, hope someone will help me!
  2. So this line gives you a NPE: "if (configSection.getConfigurationSection(w.getName()) == null) {"
    This means, that "w.getName()" causes the NPE, which means that "w" is null.
    The variable "w" is null, if "Bukkit.getWorld(args[0])" is null.
    "Bukkit.getWorld(String)" is null, if there isn't a world named "args[0]". (Javadoc)
    • Like Like x 1
  3. So I shouldn't check the ConfigurationSection but the Bukkit#getWorld ?

  4. You should only check the ConfigurationSection if the config file doesn't contain a section for each valid world.
    You must check the getWorld method's output since args[0] might not be a valid world's name.
    • Agree Agree x 1
  5. Your first arg (0) is null so you have to check the right name x)
  6. No? I guess the args variable is from the onCommand method, so the value can't be null. Even if it is, that's probably not the case, just read my answer...
    • Agree Agree x 1
  7. Worked, thanks!