2016-03-27 20 views
1

FXML dosyasını yüklemeye ve bir uygulama penceresi olarak göstermeye çalışıyorum ama bir istisna alıyorum. FXML dosyası, FXML Scene Builder tarafından oluşturuldu.İstisna Uygulama başlat yöntemi

Sup.java dosyası:

package sup; 

import java.io.IOException; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 


public class Sup extends Application { 

    @Override 
    public void start(Stage primaryStage) throws IOException { 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 



     primaryStage.setTitle("Sup"); 
     Scene scene = new Scene(root, 300, 250);   
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

} 

sample.fxml dosyası: yapının

<?import javafx.geometry.Insets?> 
<?import javafx.scene.layout.GridPane?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.text.Text?> 
<?import javafx.scene.control.TextField?> 
<GridPane fx:controller="sup.Controller" 
    xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="5"> 

    <Text text="Sup" 
     GridPane.rowIndex="0" 
     GridPane.columnSpan="2" 
     GridPane.halignment="CENTER"/> 

    <Label text="First Name:" 
     GridPane.rowIndex="1" 
     GridPane.columnIndex="0"/> 

    <TextField 
     GridPane.columnIndex="1" 
     GridPane.rowIndex="1"/> 

    <Button text="Say Sup!" 
     GridPane.rowIndex="2" 
     GridPane.columnIndex="1" 
     GridPane.halignment="RIGHT"/> 

</GridPane> 

ekran görüntüsü:

enter image description here

hata iletisi:

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:498) 
    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: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 sup.Sup.start(Sup.java:34) 
    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 
Exception running application sup.Sup 

cevap

3

Aldığınız istisnanın temel nedeni, "Konum gerekli." Ayrıntı iletisiyle birlikte NullPointerException'dur.

Caused by: java.lang.NullPointerException: Location is required. 

Ben yığın izlerini okumayı size rehberlik gitmiyorum yerine this question bakın.

null'u FXMLLoader.load() yöntemine ilettiğiniz için "konum gerekli" hatası alıyorsunuz. null değeri açıkça getResource() çağrısından gelmelidir. Class.getResource() belgelerden;

[...]

İade: bu isimde bir kaynak bulunursa
bir URL nesne veya null

[...]

Bu yüzden istisnayı elde etmenin nedeni, g soyadı bulunamadı.

doğrulamak Lütfen aşağıdaki:

standart Tutulma proje düzenini kullanıyorsanız
  • : Bir kullanıyorsanız Sup.java dosyanın
  • aynı dizine sample.fxml yerleştirilir emin olun maven benzeri dizin yapısı kaynak ve kaynaklar için farklı dizinler bulunduğundan, kaynak klasöründe ve kaynak dosyada olduğu gibi sample.fxml öğesinin bulunduğundan emin olun.

    Örnek: Kaynak dosyanız Sup.java, src/main/java/com/example/Sup.java adresinde bulunmaktadır. sample.fxml dosyasını src/main/resources/com/example/sample.fxml'a yerleştirin.

  • Eclipse kullanıyorsanız, projenizi temizleyin ve yeniden oluşturun.Oluşturma araçları benzer alternatiflere sahiptir (veya yalnızca build/out dizinini silin) ​​