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

    Johnzeh

    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:

    Code:
        static XMLReader xmlr;
        static RandomMapSelector rms;
     
        @Override
        public void onEnable() {
         
            this.getLogger().info("Core game-mode has been enabled!");
        }
     
        @Override
        public void onDisable() {
            this.getLogger().info("Core game-mode has been disabled!");
        }
     
        @Override
        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:

    Code:
        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;
            }else{
                int index = randGen.nextInt(maps.size());
                File map = (File) maps.get(index);
                return map;
            }
        }
    Stack:

    Code:
    org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    05.05 18:29:56 [Server] INFO at com.pu.rftw.main.RFTW.onCommand(RFTW.java:31)
    05.05 18:29:56 [Server] INFO Caused by: java.lang.NullPointerException
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
    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(PlayerConnection.java:109)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:840)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:883)
    05.05 18:29:56 [Server] INFO at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:965)
    05.05 18:29:56 [Server] INFO at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
    05.05 18:29:56 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
    05.05 18:29:56 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    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

    chasechocolate

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

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

    ZeusAllMighty11

    Code:
    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

    Snipey

    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