javafx.fxml.LoadException: Missing resource key

I am new to JavaFX and I am working on a simple calculator project.

I am really confused about what’s going wrong in my code. I guess there are bugs in MainController.java or main.fxml. When I run gradle to build the project,I get javafx.fxml.LoadException: Missing resource key on Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); I have checked my code with other example projects in GitHub and try to fixed bugs but failed.

Anyone has any idea why?

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: Missing resource key.
/Users/allen/IdeaProjects/Calculator/build/resources/main/allen/edu/com/main.fxml:35

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.resolvePrefixedValue(FXMLLoader.java:420)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:370)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:332)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at allen.edu.com.App.start(App.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application allen.edu.com.App

FAILURE: Build failed with an exception.

App.java

package allen.edu.com;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        Scene scene = new Scene(root);
//        scene.getStylesheets().add("org/kordamp/bootstrapfx/bootstrapfx.css");
//        scene.getStylesheets().addAll(this.getClass().getResource("styles.css").toExternalForm());
        stage.setTitle("Calculator");
        stage.setResizable(false);
        stage.setScene(scene);
        stage.show();
    }

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

}

main.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>


<AnchorPane prefHeight="327.0" prefWidth="230.0" xmlns="http://javafx.com/javafx/10.0.2-internal"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="allen.edu.com.MainController">
    <VBox layoutX="-2.0" prefHeight="327.0" prefWidth="243.0">
        <Label fx:id="output"  alignment="CENTER_RIGHT" prefHeight="81.0" prefWidth="243.0"
               textAlignment="RIGHT">
            <font>
                <Font size="64.0"/>
            </font>
        </Label>
        <GridPane prefHeight="182.0" prefWidth="243.0">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
            </rowConstraints>
            <Button fx:id="AC" prefHeight="53.0" prefWidth="60.0" text="AC" onAction="#handleButtonAction"/>
            <Button fx:id="invert" prefHeight="55.0" prefWidth="63.0" text="+/-" GridPane.columnIndex="1"
                    onAction="#handleButtonAction"/>
            <Button fx:id="percent" prefHeight="55.0" prefWidth="65.0" text="%" GridPane.columnIndex="2"
                    onAction="#handleButtonAction"/>
            <Button fx:id="divide" prefHeight="67.0" prefWidth="64.0" text="/" GridPane.columnIndex="3"
                    onAction="#handleButtonAction"/>
            <Button fx:id="seven" prefHeight="71.0" prefWidth="75.0" text="7" GridPane.rowIndex="1"
                    onAction="#handleButtonAction"/>
            <Button fx:id="eight" prefHeight="55.0" prefWidth="68.0" text="8" GridPane.columnIndex="1"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="nine" prefHeight="74.0" prefWidth="69.0" text="9" GridPane.columnIndex="2"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="multiply" prefHeight="64.0" prefWidth="65.0" text="x" GridPane.columnIndex="3"
                    GridPane.rowIndex="1" onAction="#handleButtonAction"/>
            <Button fx:id="four" prefHeight="53.0" prefWidth="70.0" text="4" GridPane.rowIndex="2"
                    onAction="#handleButtonAction"/>
            <Button fx:id="five" prefHeight="66.0" prefWidth="68.0" text="5" GridPane.columnIndex="1"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="six" prefHeight="73.0" prefWidth="83.0" text="6" GridPane.columnIndex="2"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="minus" prefHeight="66.0" prefWidth="76.0" text="-" GridPane.columnIndex="3"
                    GridPane.rowIndex="2" onAction="#handleButtonAction"/>
            <Button fx:id="one" prefHeight="90.0" prefWidth="98.0" text="1" GridPane.rowIndex="3"
                    onAction="#handleButtonAction"/>
            <Button fx:id="two" prefHeight="92.0" prefWidth="91.0" text="2" GridPane.columnIndex="1"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
            <Button fx:id="three" prefHeight="54.0" prefWidth="66.0" text="3" GridPane.columnIndex="2"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
            <Button fx:id="plus" prefHeight="71.0" prefWidth="77.0" text="+" GridPane.columnIndex="3"
                    GridPane.rowIndex="3" onAction="#handleButtonAction"/>
        </GridPane>
        <HBox prefHeight="59.0" prefWidth="243.0">
            <Button fx:id="zero" prefHeight="59.0" prefWidth="122.0" text="0" onAction="#handleButtonAction"/>
            <Button fx:id="dot" prefHeight="59.0" prefWidth="60.0" text="." onAction="#handleButtonAction"/>
            <Button fx:id="equal" prefHeight="59.0" prefWidth="63.0" text="=" onAction="#handleButtonAction"/>
        </HBox>
    </VBox>
</AnchorPane>

MainController.java

package allen.edu.com;

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

public class MainController implements Initializable {

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("Success");
    }

    private double data = 0.0;
    private String operation = "AC";

    private double readData(String input) {
        if (input.indexOf(".") != input.length()-1) {
            return Double.parseDouble(input);
        }
        return Double.parseDouble(input.substring(0,input.length()-2));
    }

    @FXML
    private Button dot;

    @FXML
    private Button one;

    @FXML
    private Button two;

    @FXML
    private Button three;

    @FXML
    private Button four;

    @FXML
    private Button five;

    @FXML
    private Button six;

    @FXML
    private Button seven;

    @FXML
    private Button eight;

    @FXML
    private Button nine;

    @FXML
    private Button AC;

    @FXML
    private Button minus;

    @FXML
    private Button plus;

    @FXML
    private Button percent;

    @FXML
    private Button divide;

    @FXML
    private Button multiply;

    @FXML
    private Button invert;

    @FXML
    private Label output;

    @FXML
    private Button zero;

    @FXML
    private Button equal;

    @FXML
    public void handleButtonAction(ActionEvent event) {
        if (event.getSource() == one) {
            output.setText(output.getText()+"1");
        } else if (event.getSource() == two) {
            output.setText(output.getText()+"2");
        } else if (event.getSource() == three) {
            output.setText(output.getText()+"3");
        } else if (event.getSource() == four) {
            output.setText(output.getText()+"4");
        } else if (event.getSource() == five) {
            output.setText(output.getText()+"5");
        } else if (event.getSource() == six) {
            output.setText(output.getText()+"6");
        } else if (event.getSource() == seven) {
            output.setText(output.getText()+"7");
        } else if (event.getSource() == eight) {
            output.setText(output.getText()+"8");
        } else if (event.getSource() == nine) {
            output.setText(output.getText()+"9");
        } else if (event.getSource() == zero) {
            if (!output.getText().equals("0"))
                output.setText(output.getText()+"0");
        } else if (event.getSource() == AC) {
            output.setText("");
            operation = "AC";
        } else if (event.getSource() == divide) {
            operation = "Divide";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == multiply) {
            operation = "Multiply";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == equal) {
            double Secondhand = readData(output.getText());
            switch (operation) {
                case "Multiply":
                    data *= Secondhand;
                    break;
                case "Minus":
                    data -= Secondhand;
                    break;
                case "Divide":
                    if (Secondhand == 0) throw new IllegalArgumentException("Argument 'divisor' is 0");
                    data /= Secondhand;
                    break;
                case "Plus":
                    data += Secondhand;
                    break;
            }
            output.setText(data+"");
        } else if (event.getSource() == plus) {
            operation = "Plus";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == minus) {
            operation = "Minus";
            data = readData(output.getText());
            output.setText("");
        } else if (event.getSource() == percent) {
            operation = "Percent";
            data = readData(output.getText());
            data /= 100;
            output.setText(data+"");
        } else if (event.getSource() == dot) {
            output.setText(data+".");
        } else if (event.getSource() == invert) {
            output.setText(""+output.getText());
        }

    }

}

Thanks.

解决方案:

The ‘%’ symbol in a string attribute is a special character in FXML, interpreted as designating a resource name: the FXMLLoader is attempting to look up a resource (but since the string terminates immediately there’s no resource name: hence the exception).

You can include a literal % using "\%", or using an expression suchas '${"%"}'.

给TA打赏
共{{data.count}}人
人已打赏
未分类

写入csv:当行中的项目为空时,列被移位 (Python)

2022-9-9 9:21:18

未分类

Blazor: 如何从一个组件向另一个组件发送值?

2022-9-9 9:21:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索