How to list all files in a directory without nullpointer exception?

Discussion in 'Plugin Development' started by Scrapnix, Apr 25, 2017.

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

    Scrapnix

    Hey there,
    I am coding a schema plugin (like a schematic plugin) and I want to list all the files with a /list command in the schemas folder of the plugin, but everytimes I get the Nullpointer exception.

    Code:
    Code:
    public boolean list(Player p) {
             File folder = new File(plugin.getDataFolder().getAbsolutePath()+"/schemas/");
             File[] listOfFiles = folder.listFiles();
    
                 for (int i = 0; i < listOfFiles.length; i++) {
                   if (listOfFiles[i].isFile()) {
                     p.sendMessage("§a" + listOfFiles[i].getName());
                   } else if (listOfFiles[i].isDirectory()) {
                     p.sendMessage("§cIn the schemas directory was found an another directory:");
                     p.sendMessage(listOfFiles[i].getName());
                     p.sendMessage("§cIt will be ignored!");
                   }
                 }
             return false;
        }
    Error:
    Code:
    [16:18:42 INFO]: Scrapnix issued server command: /schema list
    [16:18:42 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'schema' in plugin SchematicsPlugin v1.0
        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(Executors.java:511) [?:1.8.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
        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(Thread.java:745) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
        at com.Scrap.Schematics.SchematicManager.list(SchematicManager.java:77) ~[?:?]
        at com.Scrap.Schematics.Main.onCommand(Main.java:63) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    
    I hope someone can help me ;)
     
  2. Offline

    Caderape2

    @Scrapnix check if listOfFiles is not null before to loops
    listFiles() return null if no files found
     
    Scrapnix likes this.
  3. Offline

    Scrapnix

    @Caderape2
    Sorry, my mistake. I forgot to put return true into the end of the loop.

    //close
     
  4. Offline

    Zombie_Striker

    @Scrapnix
    Why are you still on 1.8? Please read this.

    If your problem has been solved, mark this thread as solved. Go to the top left of this page, click ThreadTools, edit title, and add the solved prefix.
     
Thread Status:
Not open for further replies.

Share This Page