• Nerdkid posted an update 7 months, 1 week ago

    This is probably a long shot posting this here.. but im learning java and need some help. I have been using scenebuilder to create a nice gui for a program im coding but I have stumbled on the first hurdle. What I am trying to do is very simple I want to the user to be able to input some text in a textarea and when a user presses a button it converts this to a string which I can then manipulate and send back. As I am still learning I thought id just print this string in the console for the time being. In my controller I imported my fxids that I created in scenebuilder and created the onaction methods selected in scenebuilder. The method that I am trying to run when user presses a button is this:

    void getcontent(ActionEvent event) {
    String text = inputxt.getText();
    System.out.println(text);
    }

    Simple huh?

    However for some reason when I go to run my program In intelligi I get a huge error saying:

    Exception in Application start method
    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: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
    Caused by: javafx.fxml.LoadException: Error resolving onAction=’#getcontent’, either the event handler is not in the Namespace or there is an error in the script.
    /C:/Users/admin/Documents/Textkiller/out/production/Textkiller/sample/sample.fxml:30

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    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
    Exception running application sample.Main

    However when I decide to import everything in full from scenebuilders sample skeleton controller class everything seems to work fine. It seems this bit of code makes things work:

    @FXML
    void initialize() {
    assert inputxt != null : “fx:id=\”inputxt\” was not injected: check your FXML file ‘sample.fxml’.”;

    }

    and I have no idea why and in the interested of learning I want to know. If any one has any answers id love to here them. Thanks.

    • After a little bit of searching my best guess is that this function/method “initializes data that might not be available when the constructor first runs” in other words because no content is in the textarea when you first run the program/controller this code just stops the program crashing when there isn’t anything in it? I think that’s right.. anyway hope this helps anyone running into the same problem I had because it took me ages to figure out how to make it work.

    • In scenebuilder adding text to the text field in properties will give the text a default value so you don’t need to add void initialize() to run your program. Note that this does not work with just using prompt text. Hope this helps.

      • Thank you :D! I tried posting on stackoverflow but stackoverflow programmers are the most arrogant people on earth.