JavaFX Image getting image path


I have following code:

package imagebrowser;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

 * @author Dess
public class ImageBrowser extends Application {
    Image[] images;
    ImageView[] imagesView;

public void start(Stage primaryStage) {

    StackPane root = new StackPane();

    Scene scene = new Scene(root, 600, 450);

    int j = 0;

    String path = "C://imbr/";
    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].getName().endsWith("jpg") || listOfFiles[i].getName().endsWith("JPG")) {

            files = "file:" + listOfFiles[i].getAbsolutePath();

            images[j] = new Image(files, 200, 200, true, true);
            imagesView[j] = new ImageView();


    primaryStage.setTitle("Przegladarka Obrazkow");
    for(int i = 0; i < imagesView.length; i++){


public static void main(String[] args) {


which does not work:

   Executing com.javafx.main.Main from C:\Users\Dess\Documents\NetBeansProjects\ImageBrowser\dist\run1928942616\ImageBrowser.jar using platform C:\Program Files\Java\jdk1.7.0_17/bin/java
Exception in Application start method
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.javafx.main.Main.launchApp(
    at com.javafx.main.Main.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(
    at com.sun.javafx.application.LauncherImpl.access$000(
    at com.sun.javafx.application.LauncherImpl$
Caused by: java.lang.NullPointerException
    at imagebrowser.ImageBrowser.start(
    at com.sun.javafx.application.LauncherImpl$
    at com.sun.javafx.application.PlatformImpl$
    at com.sun.javafx.application.PlatformImpl$4$
    at com.sun.javafx.application.PlatformImpl$4$
    at Method)
    at com.sun.javafx.application.PlatformImpl$
    at Method)
    ... 1 more
Java Result: 1

I know it has something to do with path of the image file, but I have no idea why. I have found examples on sites with this way of determining the path and it worked... I would apreciate some help^^

8/2/2013 12:46:37 PM

Accepted Answer

The reason is a java.lang.NullPointerException because you have not initialized your array variables:

Image[] images;            // images is null
ImageView[] imageViews;    // imageViews is also null

When accessing images like

images[j] = new Image(files, 200, 200, true, true);

a NullPointerException is thrown because images is null.

To make this work, you need to create a corresponding array object, like

Image[] images = new Image[100];    // now images is a reference to an array of 100 elements

However, I strongly discourage you to use a plain array for this - you do not know the number of images you want to load in advance (you know the maximum though, so you could still allocate an array large enough to hold the maximum number of images, but that might waste memory), so use something like an ArrayList:

List<Image> images = new ArrayList<>();

images.add(new Image(files, 200, 200, true, true));

See also Why is it preferred to use Lists instead of Arrays in Java?

5/23/2017 12:28:53 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow