NPE's when randomly selecting a file.

Discussion in 'Plugin Development' started by Johnzeh, May 5, 2013.

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


    What I want to do is randomly select a file from the 'Maps' directory. But with what I have right now causes NPE's. Can somebody help point out the possible problem?

    Main class:

        static XMLReader xmlr;
        static RandomMapSelector rms;
        public void onEnable() {
            this.getLogger().info("Core game-mode has been enabled!");
        public void onDisable() {
            this.getLogger().info("Core game-mode has been disabled!");
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            if(command.getName().equalsIgnoreCase("test")) {
                String mapname = rms.getRandomMap().getName();
                p.sendMessage("Current map name: " + mapname);
            return false;
    Random Map Selector:

        static RFTW rftw;
        static XMLReader xmlr;
        private Random randGen;
        private List<File> maps;
        public void getMaps() {
            File f = new File("/plugins/RFTW/Maps");
            File[] maps = f.listFiles();
        public File getRandomMap() {
            if(maps.size() == 0 || maps == null) {
                rftw.getLogger().severe("Failed to find maps. Check the directory.");
                return null;
                int index = randGen.nextInt(maps.size());
                File map = (File) maps.get(index);
                return map;

    05.05 18:29:56 [Server] INFO at com.pu.rftw.main.RFTW.onCommand(
    05.05 18:29:56 [Server] INFO Caused by: java.lang.NullPointerException
    05.05 18:29:56 [Server] INFO at
    05.05 18:29:56 [Server] INFO at
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.q(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.DedicatedServer.r(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.r(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.d(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.NetworkManager.b(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.Packet3Chat.handle(
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.a(
    05.05 18:29:56 [Server] INFO at
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(
    05.05 18:29:56 [Server] INFO at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(
    05.05 18:29:56 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(
    05.05 18:29:56 [Server] INFO at org.bukkit.command.PluginCommand.execute(
    05.05 18:29:56 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin RFTW v1.0
    05.05 18:29:56 [Server] SEVERE null
    05.05 18:29:54 [Server] INFO Connection reset
  2. Offline


    Your maps (List<File>) variable is null.

    EDIT: From the code you gave, a lot of your other variables are null also...
  3. Offline


    File[] f = someDirectory.listFiles();
     int select = new Random().nextInt(f.length);
    File picked = f[select];

    I don't really know what you're doing since you didn't show much.
  4. Offline


    I would like to see a working version of this as it is the only thread like it on the bukkit forum.
Thread Status:
Not open for further replies.

Share This Page