title: How to get all files from a directory using javafx
body: <p>I'm new on JavaFx and I can't manage to get all the files from a directory (I need to display some pictures using an interface built with javafx). I've been trying to use listFiles() but I keep on getting null when displayed to test.
  Here's the code :</p>
  
  <pre><code>package sample;
  
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.TextField;
  import javafx.event.ActionEvent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.image.Image;
  import javafx.scene.image.ImageView;
  import javafx.stage.Stage;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.control.*;
  import javafx.scene.shape.Polygon;
  import java.io.File;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.ResourceBundle;
  
  public class Controller implements Initializable{
  
      private String path;
      private ArrayList&lt;File&gt; files;
      private String[] extens;
  
      @FXML
      public Polygon right_arrow;
      public Polygon left_arrow;
      public Button edit_button;
      public ImageView home_button;
      public TextField search_box;
      public TextArea tag_box;
      public ChoiceBox language_box;
      public Label photo_title;
      public Label photo_number;
      public ImageView photo_box;
      public Image home_picture;
  
      public void goToEditPage (ActionEvent event) throws Exception {
          try {
              FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Edit.fxml"));
  
              Parent root1 = (Parent) fxmlLoader.load();
              Stage stage = new Stage();
              stage.setScene(new Scene(root1));
              stage.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
      public void goToHomePage (){
          try {
              FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("home.fxml"));
  
              Parent root1 = fxmlLoader.load();
              Stage stage = new Stage();
              stage.setScene(new Scene(root1));
              stage.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
  
      public Image getPicture(){
  
  
          /*home_picture = new Image(String.valueOf(getClass().getResource("home.png")));
          */return home_picture;
      }
  
      private void photo (String pathGet){
          path = pathGet;
          files = new ArrayList&lt;File&gt;();
  
          File repo = new File (path);
  
          File[] fileList = repo.listFiles();
          for (int i=0; i&lt;1; i++) {
              System.out.println(repo.list());
          }
         // System.out.println(fileList.length);
          /*int nb = fileList.length;
          String ext = "";
          int s = -1;
  
          for (int i=0; i&lt;nb; i++){
              if (fileList[i].isFile()){
                  s = fileList[i].getName().lastIndexOf(".");
                  if (s&gt;-1) {
                      ext = fileList[i].getName().substring(s + 1);
                  }
                  if (ext=="jpg"||ext=="png"||ext=="jpeg"){
                      files.add(fileList[i]);
                  }
              }
          }*/
      }
  
  
      public File getPhotoById(int id) {
          id--;
          return this.files.get(id);
      }
  
      @Override
      public void initialize(URL location, ResourceBundle resources) {
          photo("sample/photos/album_1");
          //photo_box.setImage(new Image(files.get(1).toURI().toString()));
      }
  }
  </code></pre>
  
  <p>I think I don't use listFiles() the proper way but I can't figure out how to actually do so.
  My folder album_1 contains 3 pictures in .png. However when I try this sample, I get nothing as a result, as if the folder was empty.
  I hope someone will be able to help me.
  Thank you for your time.</p>