2015-11-08 52 views
2

我對JavaFX比較新(約1個月),甚至更新到Gradle。我的項目是here, on Github。當我運行./gradlew build時,它工作正常。但是,當我運行./gradlew run,我得到這一切:Gradle with JavaFX:我做錯了什麼?

Alins-MacBook-Pro:evisu Alin$ ./gradlew run 
:compileJava UP-TO-DATE 
:processResources UP-TO-DATE 
:cssToBin SKIPPED 
:classes UP-TO-DATE 
:run 
Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
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$152(LauncherImpl.java:182) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: inputStream is null. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429) 
    at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43) 
    at ro.badilos.evisu.EviSU.start(EviSU.java:27) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$54/2140593657.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1714838540.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
Exception running application ro.badilos.evisu.EviSU 
:run FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':run'. 
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

Total time: 3.74 secs 

我在做什麼錯?

這是所有問題的基礎嗎?

Caused by: java.lang.NullPointerException: inputStream is null. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429) 
    at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43) 
    at ro.badilos.evisu.EviSU.start(EviSU.java:27) 

什麼是.fxml文件的正確路徑?

謝謝!

+0

您可以添加用於加載FXML文件的URL嗎?另外,在項目中添加FXML文件的原始位置。 – ItachiUchiha

回答

1

是的,NullPointerException是整個問題的根本原因。您資源不正確。在ro.badilos.evisu.VistaNavigator

Pane mainPane = (Pane) loader.load(getClass().getClassLoader().getResourceAsStream(VistaNavigator.MAIN)); 

,並更改以下行:用下面的

Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(VistaNavigator.MAIN)); 

:請更換此線(ro.badilos.evisu.EviSU

public static final String MAIN = "src/main/resources/Main.fxml"; 
public static final String VISTA_1 = "src/main/resources/NewSU.fxml"; 
public static final String VISTA_2 = "src/main/resources/NewSMURD.fxml"; 

到:

public static final String MAIN = "Main.fxml"; 
public static final String VISTA_1 = "NewSU.fxml"; 
public static final String VISTA_2 = "NewSMURD.fxml"; 

引入這些更改後,所有資源將被正確讀取。運行gradle clean run仍然會導致異常,但這是另一個例外。

要解決的的異常變化(ro.badilos.evisu.VistaNavigator):

public static void loadVista(String fxml) { 
    try { 
     mainController.setVista(
      (Node) FXMLLoader.load(
       VistaNavigator.class.getResource(
        fxml 
       ) 
      )); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

到:

public static void loadVista(String fxml) { 
    try { 
     mainController.setVista(
      (Node) FXMLLoader.load(
       VistaNavigator.class.getClassLoader().getResource(
        fxml 
       ) 
      )); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

記住,總是通過ClassLoader參考資源。現在它運行,但另一個NPE被打印到控制檯。

編輯

好吧,最後NPE是原因是:

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    try { 
     Image focIcon = new Image(getClass().getResourceAsStream("pool/foc16px.png")); 
     btnNewSU.setGraphic(new ImageView(focIcon)); 

     Image smurdIcon = new Image(getClass().getResourceAsStream("pool/smurd16px.png")); 
     btnNewSMURD.setGraphic(new ImageView(smurdIcon)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

應該是:

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    try { 
     Image focIcon = new Image(getClass().getClassLoader().getResourceAsStream("foc16px.png")); 
     btnNewSU.setGraphic(new ImageView(focIcon)); 

     Image smurdIcon = new Image(getClass().getClassLoader().getResourceAsStream("smurd16px.png")); 
     btnNewSMURD.setGraphic(new ImageView(smurdIcon)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

而最後一件事,你需要修復的參考Nomeclator.dbro.badilos.controller.InfoSituatiaProdusaController以及。

+0

謝謝!有效! – badilos

+0

@badilos,如果有幫助,請[接受此答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Opal