2015-02-01 4 views
9

Ich versuche, die FXML-Datei zu laden und zeigen sie als ein Anwendungsfenster, aber ich bekomme eine Ausnahme. Die FXML-Datei wurde vom FXML-Szenengenerator erstellt."Location ist erforderlich" Ausnahme beim Laden von FXML-Datei

Hier sind die Codes für die Klasse

public class Main extends Application { 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     primaryStage.setScene(FXMLLoader.load(getClass().getResource("sample.fxml"))); 
     primaryStage.show(); 
    } 
} 

und FXML Datei

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.TitledPane?> 
<?import javafx.scene.layout.AnchorPane?> 
<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
      prefHeight="400.0" prefWidth="600.0" text="Pass4D" xmlns:fx="http://javafx.com/fxml/1" 
      xmlns="http://javafx.com/javafx/8"> 
    <content> 
     <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> 
      <children> 
       <Button layoutX="211.0" layoutY="134.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="177.0" 
         text="Log in"/> 
       <Button layoutX="212.0" layoutY="170.0" mnemonicParsing="false" prefHeight="33.0" prefWidth="175.0" 
         text="Exit"/> 
      </children> 
     </AnchorPane> 
    </content> 
</TitledPane> 

Und hier ist die Ausnahme i

erhalten
Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$1/2074407503.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091) 
    at Pass4D.start(Pass4D.java:19) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/317090070.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1833150059.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/2115863517.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1436737924.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 

Was mache ich falsch?

p.s. hier ist die Projektstruktur

enter image description here

+0

Was ist die Projektstruktur? Wo ist 'sample.fxml' in Bezug auf Ihre' Main' Klasse? –

+0

hier ist es, ich aktualisierte den Beitrag – Carmine

Antwort

10

Die kurze Antwort ist getClass().getResource("sample.fxml") kehrt null still, wenn die Ressource nicht auf der Laufzeit Classpath gefunden werden kann, nicht das aktuelle Verzeichnis etc.

Das hängt also von Ihrer IDE-Projektkonfiguration ab. Wenn Sie Eclipse verwenden, versuchen Sie, den Ordner sample.fxml in der Ausführungskonfiguration hinzuzufügen.

Einige Ideen ...

  • versuchen getClass().getResource("/sample.fxml") statt ...
  • versuchen sample.fxml in den Ressourcen-Ordner zu verschieben. Ich weiß nicht viel über Ihre IDE, aber ich vermute, dass dieser Ordner nur für .java Dateien verwendet wird ... Dies ist sicherlich für Großprojekte in Eclipse - Ressourcen müssen in der src/main/resources Struktur als nur das ist, um die Laufzeit hinzugefügt werden Klassenpfad ...
+0

bewegen es zu dem Ressourcenordner half, nein, ich verstehe, was falsch war, vielen Dank!) – Carmine

+6

In Intellij, die Kombination 'Verschieben von sample.fxml in den Ressourcenordner' und' getClass(). getResource ("/ sample.fxml ")" hat für mich funktioniert. Ich musste ein "Ressourcen" Verzeichnis auf der gleichen Ebene wie der Hauptordner erstellen. –

1

Versuchen Sie dieses Beispiel von Oracle:

@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); 

    Scene scene = new Scene(root, 300, 275); 

    stage.setTitle("FXML Welcome"); 
    stage.setScene(scene); 
    stage.show(); 
} 
+1

Ich bekomme die gleiche Ausnahme – Carmine

+0

Vielleicht fehlt mir etwas in den Konfigurationen des Projekts? Das Projekt war ursprünglich nicht als JavaFX-Programm gedacht, also wurde es als leere Anwendung erstellt, vielleicht fehlt mir da etwas? – Carmine

+0

Ok, weil es für mich so funktioniert dann weiß ich nicht Entschuldigung – Johan

2

ich bereits das heute veröffentlicht, so ist hier wieder, hoffen, dass es Ihnen hilft.

Hier ist eine Lösung, die in der Entwicklungsumgebung, in Scene Builder und in einem gepackten JAR funktioniert.

Die Ordnerstruktur:

enter image description here

Main.java:

package application; 

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


public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 

      FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/RootLayout.fxml")); 
      AnchorPane rootLayout = (AnchorPane) loader.load(); 

      Scene scene = new Scene(rootLayout, 400, 400); 
      scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm()); 

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

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

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

RootLayout.fxml:

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

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

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.RootLayoutController"> 
    <children> 
     <Pane layoutX="0.0" layoutY="0.0" prefHeight="200.0" prefWidth="200.0"> 
     <children> 
      <Button fx:id="sunButton" layoutX="74.0" layoutY="88.0" mnemonicParsing="false" onAction="#handleSunButtonClick" styleClass="sun-button" stylesheets="@../css/toolbar.css" text="Button" /> 
     </children> 
     </Pane> 
    </children> 
</AnchorPane> 

RootLayoutController.java:

package application.view; 

import javafx.fxml.FXML; 
import javafx.scene.control.Button; 

public class RootLayoutController { 


    @FXML 
    Button sunButton; 

    @FXML 
    public void handleSunButtonClick() { 
     System.out.println("Button clicked"); 
    } 
} 

toolbar.css:

.sun-button { 
    -fx-graphic: url('./icons/sun.png'); 
} 

application.css:

.root { 
    -fx-background-color:lightgray; 
} 

sun.png:

enter image description here

Dies funktioniert sowohl in der Entwicklungsumgebung und, wenn Sie Paket die JAR (wählen Sie "Extrahieren benötigte Bibliotheken in generierte JAR" in Eclipse).

Screenshot (nur eine Taste mit einem Symbol über CSS geladen)

enter image description here

Verwandte Themen