Solved Null Pointer Exception on getWorldFolder()

Discussion in 'Plugin Development' started by dajako, Jul 12, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    dajako

    Code:
    Code:
    package me.dajakos.uhc;
    
    import java.io.File;
    
    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.event.Listener;
    
    public class editWorld implements Listener {
    
        private World world;
    
        public static void createWorld() {
            World world = Bukkit.getWorld("UHCWorld");
            WorldCreator creator = new WorldCreator("UHCWorld");
            creator.environment(World.Environment.NORMAL);
            creator.generateStructures(true);
            world = creator.createWorld();
        }
    
        public static void deleteWorld() {
            for (World w : Bukkit.getServer().getWorlds()) {
                if (w.getName().equals("UHCWorld")) {
                    World unload = Bukkit.getWorld("UHCWorld");
                    unloadWorld(unload);
                }
            }
    
            World delete = Bukkit.getWorld("UHCWorld");
            File deleteFolder = delete.getWorldFolder();
    
            //deleteW(deleteFolder);
        }
    
        public static boolean deleteW(File path) {
            if (path.exists()) {
                File files[] = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files.isDirectory()) {
                        deleteW(files);
                    } else {
                        files.delete();
                    }
                }
            }
            return (path.delete());
        }
    
        public static void unloadWorld(World world) {
            world = Bukkit.getWorld("");
            if (!world.equals(null)) {
                Bukkit.getServer().unloadWorld(world, true);
            }
        }
    }


    Error:

    Code:
    [17:38:01 INFO]: dajakos issued server command: /uhcstart
    [17:38:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'uhcs
    tart' in plugin UHC v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-b1e6da1-17962f6]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot.jar:git-Spigot-b1e6da1-17962f6]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1115) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :950) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :26) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :53) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got.jar:git-Spigot-b1e6da1-17962f6]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_45]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    83) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [spigot.jar:git-Spigot-b1e6da1-17962f6]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
            at me.dajakos.uhc.editWorld.deleteWorld(editWorld.java:31) ~[?:?]
            at me.dajakos.uhc.main.onCommand(main.java:48) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-b1e6da1-17962f6]
            ... 14 more


    Thanks for any replies!
     
  2. Offline

    mine-care

    @dajako
    Please follow java naming convention
    Hmm, can we see the onCommand method? because i cant see where getWorldFolder is called in the code above that has to do with the error.
     
  3. Offline

    MisterErwin

    @dajako I guess the world is null. Just use a debug line to check that.
     
  4. Offline

    dajako

    I've fixed it guys, I just entered the path manually rather than getting it from the World variable.
     
  5. Offline

    @DriftCoder

    @dajako you could solve? because I'm having the same problem
     
  6. Offline

    dajako

    Instead of:
    File deleteFolder = delete.getWorldFolder();
    I used:
    File deleteFolder = new File("UHCWorld");

    @@DriftCoder
     
  7. Offline

    @DriftCoder

    @dajako a friend of mine gave me this same solution , but thanks anyway
     
    dajako likes this.
Thread Status:
Not open for further replies.

Share This Page