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 {
                            WorldManager.getInstance().removeTitleObject(p.getWorld());
                            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(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.NullPointerException
            at brutalheroes._xxprodudexx_.worldtitles.manage.WorldManager.getTitle(WorldManager.java:100) ~[?:?]
            at brutalheroes._xxprodudexx_.worldtitles.manage.WorldManager.hasTitle(WorldManager.java:114) ~[?:?]
            at brutalheroes._xxprodudexx_.worldtitles.cmd.DelTitle.onCommand(DelTitle.java:32) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[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!
    ~Wouter
     
  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 ?

    @Trigary
     
  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!