-1

files

I'm running an FXML project and I can't load my fxml files, IntelliJ keeps indicating there is something wrong with my AnchorPane tag, however this was all generated by scene builder. This error even shows up for fxml files created by IntelliJ. The issue seems to be xmlns="http://javafx.com/javafx/19" within the AnchorPane tag at least that's where the compiler is pointing to.

I'm running IntelliJ 2022 with JDK 17.0.4.1 with JavaFX 19 using Maven, and I'm using scene builder 19.

here is my fxml code

`

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.LoginController">
    <children>
        <VBox prefHeight="400.0" prefWidth="600.0" />
        <TextField id="Username" layoutX="115.0" layoutY="60.0" prefHeight="25.0" prefWidth="126.0" promptText="user@account.com" />
        <Label layoutX="39.0" layoutY="64.0" prefHeight="17.0" prefWidth="63.0" text="Username" />
        <Label layoutX="39.0" layoutY="116.0" text="Password" />
        <TextField id="password" layoutX="115.0" layoutY="112.0" prefHeight="25.0" prefWidth="126.0" />
        <Button id="login" layoutX="132.0" layoutY="210.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="85.0" text="Login" />
        <Label layoutX="39.0" layoutY="153.0" prefHeight="17.0" prefWidth="63.0" text="Location" />
        <TextField id="Location" disable="true" layoutX="115.0" layoutY="149.0" prefHeight="25.0" prefWidth="126.0" promptText="Your location here" />
        <Label id="ErrorMessage" alignment="CENTER" disable="true" layoutX="91.0" layoutY="183.0" prefHeight="17.0" prefWidth="187.0" text="Error Label" textAlignment="CENTER" textFill="#e10724">
            <font>
                <Font name="System Italic" size="12.0" />
            </font>
        </Label>
    </children>
</AnchorPane>

`

and here is my java code

`

package controllers;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;

public class LoginController {

    @FXML
    private TextField Username;
    //Buttons
    @FXML
    private TextField Password;
    @FXML
    private TextField Location;
    @FXML
    private Label ErrorMessage;
    @FXML
    private Button Login;

    public void BeginStart(){
        //Loader
        try {
            //debugging fxml path
            URL uri = getClass().getResource("/views/login.fxml");
            System.out.println(uri);
            
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/login.fxml"));
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setTitle("Hello!");
            stage.setScene(scene);
            stage.show();

            /*Parent root = loader.load();
            Stage stage = new Stage();
            stage.setTitle("Scheduler Login");
            stage.setScene(new Scene(root));
            stage.show();*/
        }catch(Exception e){
            System.out.println("Failed to load login.fxml");
            System.out.println("Exception: " + e.toString());
        }
    }
}

`

uri is non-null so there appears to be no problem with the file path.

Previously I've had the "uri not registered" error before I deleted the javaFX files included with Maven, and used a local copy of the JavaFX19 library, along with the proper JVM arguments.

Note: the image I included is from a previous version of my project. Under resources "controllers" is actually supposed to be "views".

  • 2
    please add the complete stacktrace (formatted as code) and unrelated: stick to java naming conventions when showing java code publicly – kleopatra Nov 09 '22 at 10:46
  • also: missing fx:id for all your fields, meaning that nothing is injected (as fields, at least) – kleopatra Nov 09 '22 at 10:49
  • 3
    You’re suppressing the debugging information required to diagnose the problem. Print the complete stack trace from the exception you catch. The `toString()` method only gives you the message, which is not going to tell you anything other than “something went wrong”. The stack trace will tell you exactly what went wrong, and where. – James_D Nov 09 '22 at 11:42
  • 2
    See https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – James_D Nov 09 '22 at 12:34
  • 1
    `fx:id` != `id` (though the former will set the latter if only the former is present). Side note: In Java, the standard naming conventions have fields and method names using `camelCase`, not `PascalCase`. – Slaw Nov 09 '22 at 15:27
  • 1
    There is no file `/views/login.fxml`, this could never have worked as posted. – jewelsea Nov 09 '22 at 18:05
  • The [URI not registered](https://stackoverflow.com/questions/42569634/uri-is-not-registered-settings-languages-frameworks-schemas-and-dtds-in) message shown by Idea in this instance is irrelevant. It is not a real error, it has nothing to do with the runtime of the code. There is no official schema for JavaFX FXML as FXML works on the reflection of code, so the types in it are dynamic rather than static and a full schema for FXML could never be created. – jewelsea Nov 09 '22 at 22:09
  • That's interesting that the fx:id is missing in those fields, this was generated by Scene Builder. – Brenden Delong Nov 10 '22 at 06:15
  • The section in the [makery tutorial](https://code.makery.ch/library/javafx-tutorial/part2/) titled “Hook the View to the Controller” demonstrates correctly assigning `fx:id` values in SceneBuilder. – jewelsea Nov 10 '22 at 06:23
  • My use of PascalCase was to distinguish between the JFX fields, and local/class variables. Also I must have changed /views/login.fxml to /controllers/login.fxml before I posted, so you are correct. – Brenden Delong Nov 10 '22 at 06:27
  • You're right, I added the id in the wrong field. If I had looked under the "code" section I would have seen fx:id field. Thanks! – Brenden Delong Nov 10 '22 at 06:45

1 Answers1

0

Ok, so the issue was with the fx:controller reference. My controller class was not within src/main/java/com.example.scheduler.

After moving it there the error ceased.

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25