2017-04-16 53 views
0

我想让我的项目一个可运行的jar但问题是,gui没有显示出来。从我的cmd中的错误,我告诉它是关于我的图像浏览。Java Runnable jar与ImageView不显示GUI

我的项目包含位于除sourcefoler之外的另一个文件夹中的图像视图。我已经设置了构建路径,但它似乎仍然没有工作

这是我的图像添加到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); 
     } 

文件夹imageTest添加为sourcefolder在构建路径

这是通过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" 

这是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); 

    } 

} 

要清楚...该程序通过月食laucnhing时工作,但没有当我让它运行的JAR 这是系统从日食 Link

+0

的【什么是一个NullPointerException,如何解决呢?(可能的复制http://stackoverflow.com/questions/218384/what-is-a- nullpointerexception-and-how-do-i-fix-it) – Seelenvirtuose

回答

1

你的变量打开时,它的外观listOfFiles为空。请参阅:https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles--

返回NULL如果此抽象路径名不表示一个目录, 或者发生I/O错误。

  • 检查变量path与现有的目录进行初始化。
  • 检查您的可执行文件是否有权读取目录。
  • 检查该目录包含文件
+0

我同意它必须是因为listOfFiles为null,并且这将意味着文件夹imageTest的构建路径不起作用。那么我该如何正确地做到这一点?我的日蚀似乎很容易找到方式 –

+0

你可以阅读这篇文章来学习不同的方法来加载你的资源:http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an -inputstream – fandango