简体   繁体   中英

How to fix a javafx.fxml.LoadException

I'm looking at the javafx example code the LoginDemo. I used the code for multiply scene application; and I get a javafx.fxml.LoadException. I don't know what to do.

My code is to open a window and have a button and click it to display text in a javafx TextArea.

Main:

package Test;

import java.io.InputStream;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

private Stage stage;

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

@Override
public void start(Stage primaryStage) throws Exception {
    try{
        stage = primaryStage;
        stage.setTitle("Test");
        setScene();
        stage.show();
    }catch(Exception e){
        e.printStackTrace();
    }

}

private void setScene(){
    try{
        Cont controller = (Cont) changeScene("Window.fxml");
        controller.setApp(this);
    }catch(Exception e){
        e.printStackTrace();
    }
}

private Initializable changeScene(String fxml) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    InputStream in = Main.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(Main.class.getResource(fxml));
    AnchorPane page;
    try {
        page = (AnchorPane) loader.load(in);
    } finally {
        in.close();
    } 
    Scene scene = new Scene(page, 800, 600);
    stage.setScene(scene);
    stage.sizeToScene();
    return (Initializable) loader.getController();
   }

}

Cont:

package Test;

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.TextArea;


public class Cont implements Initializable {

private Main application;

@FXML
private Button btn;
@FXML
public TextArea Console;

public void setApp(Main application){
    this.application = application;
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    assert btn != null : "fx:id=\"btn\" was not injected: check your FXML file 'Window.fxml'.";
    assert Console != null : "fx:id=\"Console\" was not injected: check your FXML file 'Window.fxml'.";

    btn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent arg0) {
            setText("hello world");
        }
    });

}

public void setText(String text){
    Console.appendText(text+"\n");
 }

}

Window.fxml:

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

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


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="Cont">
   <children>
      <TextArea fx:id="Console" prefHeight="365.0" prefWidth="600.0" />
      <Button fx:id="btn" layoutX="274.0" layoutY="374.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

The Error:

javafx.fxml.LoadException: 
/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at Test.Main.changeScene(Main.java:49)
at Test.Main.setScene(Main.java:35)
at Test.Main.start(Main.java:25)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/70604542.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/455370116.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.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.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Cont
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 24 more

The fx:controller attribute requires the fully qualified class name of the controller class. Since your controller Cont is in a package called Test , you need

fx:controller="Test.Cont"

The stack trace really gives you all the information you need here: the file and line number where the error originates:

/C:/Users/user/workspace%20for%20coding/Javafx-Test/bin/Test/Window.fxml:9

and the underlying cause:

Caused by: java.lang.ClassNotFoundException: Cont

ie it can't find the class called Cont (because its name is Test.Cont ).

(As an aside, note that package names should be all lower case, so you really should call the package test , not Test .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM