Java TextArea setText() 和 appendText() 返回空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26762905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java TextArea setText() and appendText() returning Null Pointer Exception
提问by senex_subconscious
I am using JavaFX 8 and FXML in this project and trying to update my text area with the results from other classes within the program.
我在这个项目中使用 JavaFX 8 和 FXML,并尝试使用程序中其他类的结果更新我的文本区域。
The Text Area is defined in the FXML document as such:
文本区域在 FXML 文档中定义如下:
<TextArea fx:id="feedBack" editable="false" focusTraversable="false"
layoutX="203.0" layoutY="32.0" prefHeight="205.0" prefWidth="308.0"
wrapText="true"/>
It is called in the controller here, note that originally it was a "private" item but to make it work with the classes I made it public:
它在这里的控制器中被调用,请注意,最初它是一个“私有”项目,但为了使其与我公开的类一起使用:
@FXML
public static TextArea feedBack;
EDIT: It is worth noting that when the TextArea is identified as "private" I have no issue getting the set/append text method to work but this does not allow me to use the text area in other classes, which is what I need to do.
编辑:值得注意的是,当 TextArea 被标识为“私有”时,我可以让 set/append 文本方法正常工作,但这不允许我在其他类中使用文本区域,这是我需要的做。
However now when I try to do either appendText() or setText(), such as follows:
但是现在当我尝试执行 appendText() 或 setText() 时,如下所示:
feedBack.setText("Connection Made");
I am given a null point exception. Why is this? I have made this work in JavaFX 7 with FXML by doing the same thing and it works fine. What am I missing to make this work?
我得到了一个空点异常。为什么是这样?我通过做同样的事情在带有 FXML 的 JavaFX 7 中完成了这项工作,并且工作正常。我缺少什么才能完成这项工作?
EDIT, test/proof of brokenness with complete tiny program. I did not make another class for this one as the textarea won't work in the control anyway.
编辑,用完整的小程序测试/证明损坏。我没有为此创建另一个类,因为无论如何 textarea 都不会在控件中工作。
FXML:
文件格式:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatester.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TextArea fx:id="feedback" layoutX="61.0" prefHeight="200.0" prefWidth="200.0" wrapText="true" />
<Button fx:id="dostuff" layoutX="261.0" layoutY="62.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
Main Method:
主要方法:
package textareatester;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TextAreaTester extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Controller Class:
控制器类:
package textareatester;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
public static TextArea feedback;
@FXML
private Button dostuff;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
feedback.setText("Hello World!"); ///<-- this is what gives me the NPE
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// not touching anything
}
}
Solution for those who are curious:I ended up simply returning my results strings from my methods and then appending them to the TextArea.
对于那些好奇的人的解决方案:我最终只是从我的方法中返回我的结果字符串,然后将它们附加到 TextArea。
采纳答案by Turing85
The problem is the static
at the definition of the TextArea
. Since static
attributes are bound to the class and not the object, feedback gets never initiated and is null
when accessed in the EventHandler.
问题是static
在的定义TextArea
。由于static
属性绑定到类而不是对象,因此反馈永远不会启动,而是null
在 EventHandler 中访问时。