编程语言
首页 > 编程语言> > 应用程序构造函数中的JavaFX异常(找不到此类方法异常)

应用程序构造函数中的JavaFX异常(找不到此类方法异常)

作者:互联网

我正在尝试为正在编辑源代码的聊天系统将swing UI转换为JavaFX,并且在尝试运行该程序时遇到错误.
这是我的主班:

public class Main extends Application{

    private Main() {

    }
    private static ArgumentParser argumentParser;
    private static Stage primaryStage;
    private static ArgumentResponder argumentResponder;
    private static UncaughtExceptionLogger uncaughtExceptionLogger;
    private static Settings settings;
    /**
     * The main method, for starting the application.
     *
     * <p>See {@link Argument} for the supported arguments.</p>
     *
     * @param args arguments given when starting KouChat.
     */
    public static void main(String[] args){

        argumentParser = new ArgumentParser(args);
        argumentResponder = new ArgumentResponder(argumentParser);

        if (!argumentResponder.respond()) {
            return;
        }

        new LogInitializer(argumentParser.hasArgument(Argument.DEBUG));
        // Initialize as early as possible to catch all exceptions
         uncaughtExceptionLogger = new UncaughtExceptionLogger();

         settings = loadSettings(argumentParser);

        launch(args);
    }
    private static Settings loadSettings(final ArgumentParser 
argumentParser) {
        final Settings settings = new Settings();

        final ArgumentSettingsLoader argumentSettingsLoader = new 
ArgumentSettingsLoader();
        argumentSettingsLoader.loadSettings(argumentParser, settings);

        final PropertyFileSettingsLoader propertyFileSettingsLoader = new 
PropertyFileSettingsLoader();
        propertyFileSettingsLoader.loadSettings(settings);

        return settings;
    }
    @Override
    public void start(Stage primaryStageObj) throws Exception{
        primaryStage = primaryStageObj;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/swing 
/Chat.fxml"));
        ChatController pls = new ChatController(argumentParser, settings, 
uncaughtExceptionLogger);
        loader.setController(pls);
        pls.setStage(primaryStage);
        Parent root = (Parent) loader.load();
        primaryStage.setTitle("Flake");
        primaryStage.setScene(new Scene(root, 959,583  ));
        primaryStage.setResizable(false);
        primaryStage.show();
        primaryStage.setResizable(false);
        primaryStage.setOnCloseRequest(e -> Platform.exit());


    }
}

这是我的控制器:

public class ChatController {
    @FXML
    private TextArea messageBox;
    @FXML
    private Label onlineCountLbl;
    @FXML
    private ListView userList;
    @FXML
    private Button fileBtn;
    @FXML
    private Button sendBtn;
    @FXML
    private ScrollPane scrollPane;
    @FXML
    private ListView messageArea;


    private final UITools uiTools = new UITools();
    //private final User me;
    private Stage stage;

    private ListView<String> messageList;
    private UIFactory Flake;


    public ChatController() {
    }



    private boolean done;

    public ChatController(final ArgumentParser argumentParser,final Settings 
settings,final UncaughtExceptionLogger uncaughtExceptionLogger){
        Validate.notNull(argumentParser, "Argument parser can not be null");
        Validate.notNull(settings, "Settings can not be null");
        Validate.notNull(uncaughtExceptionLogger, "Uncaught exception logger 
can not be null");
        Flake = new UIFactory(scrollPane, userList, onlineCountLbl, 
messageBox, messageArea, argumentParser,settings, uncaughtExceptionLogger);



    }

    public void setStage(Stage stage){
        this.stage = stage;
    }

}

这是我的错误信息:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class net.usikkert.kouchat.Main
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoSuchMethodException: net.usikkert.kouchat.Main.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getConstructor(Class.java:1825)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:818)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more

我不知道如何解决此问题,有没有一种方法可以使用户使用swing更好地使用UI,因为从网上看到的所有人都说我应该使用JavaFX,因为JavaFX看起来比swing更好,并且更易于使用.

解决方法:

java.lang.NoSuchMethodException: package.Class.<init>()

当无法调用没有参数的构造函数时,将发生此异常.大多数情况下,发生这种情况是因为没有提供参数的构造函数,而您的问题是构造函数是私有的.

设置这条线

private Main() {

}

public Main() {

}

并且错误应该得到解决.

标签:javafx,runtime-error,java
来源: https://codeday.me/bug/20191025/1927375.html