2016-11-19 473 views
1

我無法弄清楚這段代碼的問題。我已經在這裏做了許多類似的問題的研究,解決目錄是否正確,可能的錯誤函數調用等。FXMLLoader.load的JavaFX錯誤(getClass()。getClassLoader()。getResource(「Login.fxml」));

我希望有人能幫助我。一切都在名爲loginapp的應用程序中名爲login的文件中。

這裏是Login.java

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package login; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 


public class Login extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml")); 
     Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.setTitle("Fracken"); 
     stage.show(); 
    } 
} 

這裏是Login.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="317.0" prefWidth="326.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="login.Login"> 
    <children> 
     <TextField fx:id="txtUsername" layoutX="110.0" layoutY="45.0" promptText="Username" /> 
     <PasswordField fx:id="txtPassword" layoutX="110.0" layoutY="115.0" promptText="Password" /> 
     <Button fx:id="btnLogin" layoutX="110.0" layoutY="184.0" mnemonicParsing="false" onAction="btnLoginAction" text="Login" /> 
     <Button fx:id="btnReset" layoutX="232.0" layoutY="184.0" mnemonicParsing="false" onAction="btnResetAction" text="Reset" /> 
     <Label fx:id="lblMessage" layoutX="110.0" layoutY="236.0" prefHeight="31.0" prefWidth="187.0" /> 
    </children> 
</AnchorPane> 

我相信這個問題是

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("Login.fxml")); 

我得到這個錯誤。

Executing C:\Users\David\Desktop\Java Project\loginapp\dist\run122343396\loginapp.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java 
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(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at login.Login.start(Login.java:23) 
    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 
Java Result: 1 

請幫助我,我在這裏看過其他類似的問題,但沒有解決方案的工作。

+0

錯誤說「位置是必需的」,這只是表示您的FXML文件的路徑是錯誤的。 –

+0

路徑是什麼,絕對的包括C:/或者是本地的/login/Login.fxml。我試過多條路徑。 – FamousFrik

+0

路徑是相對於當前類的位置的url。什麼包是'Login.fxml'中的? ('FXMLLoader'期待與'Login'類相同的包,即在'login'中。)FXML文件是否被部署到jar文件中? –

回答

3

getClass().getResource("Login.fxml")應該返回一個非空值,因爲使用Class.getResource查找相對於該類的資源。由於您似乎已將資源置於login包中,因此使用資源名稱爲Login.fxml的該包中的類應可行。

由於某種原因,您不使用將此資源URL傳遞給的FXMLLoader。相反,您使用

getClass().getClassLoader().getResource("Login.fxml") 

也就是說,您使用類加載器來加載文件。然而,類加載器不知道您從中檢索它的類,因此嘗試在默認包中找到Login.fxml

如果你使用一個類加載器,你應該使用完整路徑,即

getClass().getClassLoader().getResource("login/Login.fxml") 
+0

謝謝。我已經嘗試了一切,這工作。這是它爲什麼起作用的一個很好的解釋。澄清它非常!我會補充說,在Netbeans中,完整路徑往往是packagename/file.fxml – FamousFrik

1

我FXML文件是不是在同一個文件夾中的main(既不在同一個包)。我得到的唯一方法是使用:getClass()。getResource(「/ fxml/inicial.fxml」);

-2

只需添加用戶庫。來自java-jre-lib-javafx的javafx。

+0

爲什麼/如何幫助找到_resource_(又名:fxml文件)? – kleopatra

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18887177) – meltedspark