如何从多个文本字段中获取文本?Net beans和Scene Builder



我在从多个文本字段中获取文本时遇到问题。我正在使用带有场景生成器的NetBeans作为UI扩展。每次运行程序时,我都会收到以下错误:

线程"JavaFX应用程序线程"java.lang.RuntimeException中的异常:java.lang.reflect.InvocationTargetException

我已将文本字段初始化如下:EmailController.java

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

当我按下"发送"时,我想打印我从文本字段中得到的文本。

txtMessage是一个多行文本字段。

EmailController.java

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

任何帮助都将不胜感激。

原因:

由java.lang.reflect.InvocationTargetException引起在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)在sun.reflect.mic.Trampoline.reinvoke(MethodUtil.java:71)位于sun.reflect.GeneratedMethodAccessor1.invoke(未知源)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)在sun.reflect.misc.MethodUtil.ioke(MethodUtil.java:275)位于javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)…还有48导致原因:java.lang.NullPointerException位于clientv2.pkg0.EmailController.handleSendAction(EmailControll.java:46)…还有58个

电子邮件.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>

TextFieldTextArea是两个不同的控件。在FXML文件中,她声明了TextArea,但在控制器中注释了TextField

让代码疯狂

@FXML private TextField txtMessage;

@FXML private TextArea txtMessage;

最新更新