FXML Load exception
Asked Answered
M

5

13

I got a problem that I absolutely can't solve on my own because I have just started using JAVA FX. I get a nasty javafx.fxml.LoadException: , but I have done exactly like a guide, but I cant get my Main to run. This is the the exception output:

apr 07, 2014 4:06:37 EM application.Main start
ALLVARLIG: null
javafx.fxml.LoadException: 
/C:/Users/Jakob/Dropbox/java_kurser/Project%20Timeline/bin/application/LoginGUI.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:21)
    at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6]
Message: Bearbetningsinstruktionens målmatchning "[xX][mM][lL]" är inte tillåten.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
    ... 20 more

LoginController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;


public class LoginController implements Initializable {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="loginButton"
    private Button loginButton; // Value injected by FXMLLoader

    @FXML // fx:id="newUserButton"
    private Button newUserButton; // Value injected by FXMLLoader

    @FXML // fx:id="passwordField"
    private PasswordField passwordField; // Value injected by FXMLLoader

    @FXML // fx:id="usernameField"
    private TextField usernameField; // Value injected by FXMLLoader

    public void initialize(URL location, ResourceBundle resources) {

        assert loginButton != null : "fx:id=\"loginButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert newUserButton != null : "fx:id=\"newUserButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert passwordField != null : "fx:id=\"passwordField\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert usernameField != null : "fx:id=\"usernameField\" was not injected: check your FXML file 'LoginGUI.fxml'.";


        //The button event for the login button
        loginButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e)   {
                System.out.println("This button works");
            }
        });
        }
    }

LoginGUI.fxml

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


<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?scenebuilder-background-color 0x008effff?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="LoginController">
  <children>
    <Button fx:id="loginButton" cancelButton="false" contentDisplay="CENTER" defaultButton="true" layoutX="254.0" layoutY="263.0" mnemonicParsing="false" opacity="1.0" prefHeight="28.0" prefWidth="92.0" text="Login" underline="false">
      <font>
        <Font size="14.0" fx:id="x1" />
      </font>
    </Button>
    <PasswordField fx:id="passwordField" layoutX="241.0" layoutY="206.0" prefHeight="23.000099999997474" prefWidth="118.0" promptText="Password" />
    <TextField fx:id="usernameField" layoutX="241.0" layoutY="166.0" prefHeight="23.0" prefWidth="118.0" promptText="Username" />
    <Label layoutX="248.0" layoutY="98.0" prefHeight="35.000099999997474" prefWidth="105.0" text="Welcome">
      <font>
        <Font size="22.0" />
      </font>
    </Label>
    <Button id="loginButton" fx:id="newUserButton" cancelButton="false" contentDisplay="CENTER" defaultButton="true" font="$x1" layoutX="254.0" layoutY="313.0" mnemonicParsing="false" opacity="1.0" prefHeight="28.0" prefWidth="92.0" text="New User" underline="false" />
  </children>
</AnchorPane>

and Main.java

package application;

import java.util.logging.Level;
import java.util.logging.Logger;

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 {

    public static void main(String[] args) {
        Application.launch(Main.class, (java.lang.String[])null);
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Bluerift Timeline");
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
Mutism answered 7/4, 2014 at 14:10 Comment(6)
Please translate: Bearbetningsinstruktionens målmatchning "[xX][mM][lL]" är inte tillåten.Moolah
The processing instructions goal match........ is not allowed. And in the beginning: ALLVARLIG: null = SERIOUS : nullMutism
Not sure this is the problem, but the main method is unusual. You can just do launch(args);. Whatever you do, though, passing in a null reference for the args array is a pretty bad idea. Application.launch(Main.class, new String[0]); would be much safer if you wanted (for some reason) to force it to ignore command line arguments.Corollary
@Corollary Oh you are an angel ;) Thanks. I wrote as you suggested with new String[0] and now it launched! :D :D Thanks again :D Now i know that for sure and hopefully wont do the same mistake again.Mutism
Ah Freaking Java fx. I hate it now :( Now its the same problem as before, but i have it exactly like i had it yesterday when it worked. I dont know what i will do. throw out my computer maybeMutism
check your IDE settings and try to rebuild the project, delete the production files etc.Dwayne
N
16

The problem is in source

So, you've got to change it to a proper one

So do that, edit the source of the fxml file here

AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));

with this one

AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("/packagename/LoginGUI.fxml"));
Numeral answered 27/5, 2014 at 16:58 Comment(0)
I
6

I had the same problem today and for those who may have it and (like me) are looking for an answer for that:

You may have changed any component Id on your Controller file and forgot to update the Id on sceneBuilder (or FXML file)[or Vice-Versa], so the Controller can't link the component on View file.

For Exemple: (On Controller file)

@FXML
Button btnName1

(On FXML file or SceneBuilder)

fx:id="btnName_1"

:-)

Inconsistent answered 17/10, 2018 at 9:35 Comment(0)
U
5

I experienced this issue too. I found that I did not add the package name to the controller name given in the FXML file; I initially just added the Controller class name. For example: if my controller class is under com.stackoverflow.gui package and the name of my controller class is LoginController. The FXML file should have com.stackoverflow.gui.LoginController and not just LoginController

Urena answered 10/12, 2017 at 10:1 Comment(0)
C
0

I resolved this error by changing the file: "module-info.java".

module your_project_name {     
   requires javafx.controls;     
   requires javafx.fxml;     
   opens application to javafx.graphics, javafx.fxml;     
   exports gui to javafx.fxml;     
   opens gui to javafx.fxml; 
}
Champion answered 1/2 at 19:59 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Incommode
A
0

In my case, the problem was in the controller initialize() method. It can be seen that FXMLLoader considers it his. I changed the name and it worked.

Anastomosis answered 29/6 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.