Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Exception in Application constructor(No Such method found exception)

I am trying to convert a swing UI to a JavaFX one for a chat system that I am editing the source code for and I am getting error when I try to run the program. This is my Main Class:

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());


    }
}

This is my Controller:

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;
    }

}

This is my error message:

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

I don't know how to fix this is there a way to make the UI look better using swing because from what I saw online people are all saying I should use JavaFX since it looks much better and is easier to work with than swing.

like image 270
forests Avatar asked Mar 26 '18 06:03

forests


1 Answers

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

This Exception occurs when a constructor without parameters cannot be called. Most of the times it happens because there is no constructor without parameters provided, in your case the problem is that the constructor is private.

Set this line

private Main() {

}

to

public Main() {

}

and the error should be resolved.

like image 127
Absent Avatar answered Oct 26 '22 18:10

Absent