Jennifer Bester
Jennifer Bester

Reputation: 15

How to get the text from multiple text fields? Net beans and Scene Builder

I have a problem with getting text from multiple text fields. I'm using Net Beans with Scene Builder as an UI-Extension. Every time I run the program, I get the following error:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

I have initialized my text fields' as follows: EmailController.java

@FXML private TextField txtTo;
@FXML private TextField txtSubject;
@FXML private TextField txtMessage;

And when I press 'send' I want to print the text I got from my text fields.

txtMessage being a multiline textfield.

EmailController.java

@FXML
private void handleSendAction(ActionEvent event) {
    System.out.println(txtTo.getText());
    System.out.println(txtSubject.getText());
    System.out.print(txtMessage.getText());
}

Any help would be much appreciated.

Caused by:

Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) ... 48 more Caused by: java.lang.NullPointerException at clientv2.pkg0.EmailController.handleSendAction(EmailController.java:46) ... 58 more

Email.fxml

<AnchorPane id="AnchorPane" prefHeight="375.0" prefWidth="600.0" style="-fx-background-color: #d3d3e8;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="clientv2.pkg0.EmailController">
   <children>
      <TextField fx:id="txtTo" layoutX="5.0" layoutY="36.0" prefHeight="30.0" prefWidth="590.0" promptText="TO:" />
      <TextField fx:id="txtSubject" layoutX="5.0" layoutY="66.0" prefHeight="30.0" prefWidth="590.0" promptText="SUBJECT:" />
      <TextArea fx:id="txtMessage" layoutX="5.0" layoutY="96.0" prefHeight="240.0" prefWidth="590.0" promptText="Enter Text Here..." />
      <Button fx:id="btnSend" layoutX="477.0" layoutY="342.0" mnemonicParsing="false" onAction="#handleSendAction" prefHeight="25.0" prefWidth="118.0" style="-fx-background-color: #bcb1cc;" text="Send" />
      <Label layoutX="273.0" layoutY="2.0" text="Email">
         <font>
            <Font size="23.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

Upvotes: 0

Views: 211

Answers (1)

mr mcwolf
mr mcwolf

Reputation: 2859

TextField and TextArea are two different controls. In the FXML file she declared that TextArea but in the controller annotated TextField.

For the code to go mad

@FXML private TextField txtMessage;

With

@FXML private TextArea txtMessage;

Upvotes: 1

Related Questions