0

I am trying to make my project a runnable jar but the problem is that the gui is not showing up. From the error in my cmd i tell it is something about my imageviews.

My project contains imageviews which are located in another folder than the sourcefoler. I have set the buildpath, but it still doesn't seem to work

This is where I add the images to the GUI:

String path = "./imageTest/";
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) { <----- Line 51
            ImageView imageView;
            imageView = createImageView(file);
            tile.getChildren().addAll(imageView);
        }

The folder imageTest is added as a sourcefolder in the buildpath

This is the error I get when running the jar through cmd:

"Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
        at application.guifx.MainScreen.mainScene(MainScreen.java:51)
        at application.guifx.MainApp.start(MainApp.java:37)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
        ... 1 more"

This is the MainApp

public class MainApp extends Application {
    private Service service;
    private Stage stage;
    private ScrollPane root = new ScrollPane();

    private MainScreen mainScreen; 

    public MainApp() {
        service = Service.getService();
        StorageInitializer.initStorage();

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        service = Service.getService();

        stage = primaryStage;

        mainScreen = new MainScreen(stage);

        stage.setWidth(100);
        stage.setHeight(100);

        primaryStage.setWidth(1000);
        primaryStage.setHeight(850);

        Scene scene = mainScreen.mainScene();<----- Line 37

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);

    }

}

To be clear... The program works when laucnhing through eclipse, but doesn't when I make it a runnable jar This is how the system looks when opening it from eclipse Link

  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Seelenvirtuose Apr 16 '17 at 17:40

1 Answers1

1

Your variable listOfFiles is null. See : https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles--

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

  • Check if the variable path is initialized with an existing directory.
  • Check if your executable have rights to read the directory.
  • Check if the directory contains files
cactuschibre
  • 1,908
  • 2
  • 18
  • 36
  • I agree that the it must be because the listOfFiles is null, and that will mean that the build path to the folder imageTest doesn't work. So how do I do that correctly? My eclipse seems to find the way easily – Mads Stoltenborg Apr 16 '17 at 17:51
  • You can read this post to learn differents methods to load your resources : http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – cactuschibre Apr 16 '17 at 17:54