Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

Trying to call a JavaFX application from Java... NoSuchMethodException

I have a main class which should call a JavaFX application (SimpleSun) to get Information from the user. Currently I create an Object of the JavaFX class and start it, but that doesn't seem to work. Does someone see the mistake in my work?

Here's my code and exception: Main.java:

package ch.i4ds.stix.sim;

import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;


    public class Main{
    Configuration config;
    public static void main(String[] args) {
        ConfigurationFromFile config = new ConfigurationFromFile();
        SimpleSun ss = new SimpleSun(config);
        ss.show();
    }
}

SimpleSun.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;

public class SimpleSun extends Application{

    private Stage primaryStage;
    Configuration configuration;

    public SimpleSun(ConfigurationFromFile config) {
        this.configuration = config;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Simple Sun - Alpha");
        System.out.println("Test");
        try {
            // Load the root layout from the fxml file
            FXMLLoader loader = new FXMLLoader(
                    Main.class.getResource("view/RootLayout.fxml"));
            BorderPane rootLayout = (BorderPane) loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            // Exception gets thrown if the fxml file could not be loaded
            e.printStackTrace();
        }
    }

    public void show(){
        launch();
    }

}

Exception:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class ch.i4ds.stix.sim.SimpleSun
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: ch.i4ds.stix.sim.SimpleSun.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    ... 4 more
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must provide a constructor with no arguments when you extend application. So you could do something like:

public class SimpleSun extends Application {

    private Stage primaryStage;
    Configuration configuration;

    public SimpleSun() {
        this.configuration = Main.getConfig();
    }
    //...

and in your Main class:

public static Configuration getConfig() { return new ConfigurationFromFile(); }

Alternatively you can pass String parameters to the class with launch(args) and get them back in the SimpleSun class with getParameters().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...