Soy nuevo con javafx y tengo un error que no puedo resolver, tengo un textarea en el que quiero mostrar el texto del archivo que abra
sample.fxml MenuBar
<MenuBar fx:id="menu_bar" prefHeight="25.0" prefWidth="609.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<menus>
<Menu fx:id="menu_file" mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="item_open" text="Open" onAction="#onOpen" />
<MenuItem fx:id="item_save" mnemonicParsing="false" text="Save" onAction="#onSave" />
<MenuItem fx:id="item_saveAs" mnemonicParsing="false" text="Save as" onAction="#onSaveas"/>
<MenuItem fx:id="item_new" mnemonicParsing="false" text="New" onAction="#onNew" />
<MenuItem fx:id="item_exit" mnemonicParsing="false" text="Salir" onAction="#onClose" />
</items>
</Menu>
sample.fxml TextArea
<AnchorPane fx:id="panel_textArea" layoutX="27.0" layoutY="76.0" prefHeight="249.0" prefWidth="541.0">
<children>
<TextArea fx:id="text_area" layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="528.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<ScrollBar layoutX="304.0" layoutY="221.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="235.0" />
<ScrollBar layoutX="520.0" layoutY="35.0" orientation="VERTICAL" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
Controller.java Al momento de seleccionar "Open" se despliega un FilChooser, selecciono un archivo y con buffered reader leo ese achivo y cuando intento escribir las lineas leidas sobre el textarea sale el error
public class Controller {
@FXML
static TextArea text_area;
@FXML
private void onOpen()
{
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files", "*.txt"),
new ExtensionFilter("PDF Files","*.pdf"),
new ExtensionFilter("dat Files","*.dat")
);
String path_name = fileChooser.showOpenDialog(new Stage()).getPath();
if (path_name != null) {
try (BufferedReader br = new BufferedReader(new FileReader(path_name))) {
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
System.out.println(strCurrentLine);
//text_area.appendText(strCurrentLine+"\n"); //-> aqui es donde pretendo agregar lo que lee mi bfr
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Error:
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1859)
... 42 more
Caused by: java.lang.NullPointerException
at sample.Controller.onOpen(Controller.java:36)
... 53 more