JavaFX: Null Pointer Exception when Setting TextArea Content in Second Window from First Window

44 Views Asked by At

I am using JavaFX and creating two window application one to browse a file and the other to show its content

the Problem is that I am getting null pointer exception when I acssess inputText.setText(filecontent) As it says inputText is null


public class AppController  {


    private Button BrowseButton;


    @FXML
    private void handleBrowseButtonAction(MouseEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Choose a File");
        File selectedFile = fileChooser.showOpenDialog(BrowseButton.getScene().getWindow());

        if (selectedFile != null) {
            try {
                String fileContent = Files.readString(selectedFile.toPath());
                openFullScreenStage(fileContent);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private void openFullScreenStage(String fileContent) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("editor.fxml"));
            Parent root = loader.load();

            // Accessing the controller of the editor.fxml
            editorController editorController1 = loader.getController();

            // Assuming TextArea in editor.fxml has the fx:id "inputText"
            /*TextArea textArea = editorController1.inputText;*/

            //editorController1.initialize();
           // editorController1.fakeinitialize();

            editorController1.setInputText(fileContent);



                Stage fullScreenStage = new Stage();
                fullScreenStage.setFullScreen(true);
                fullScreenStage.setScene(new Scene(root));
                fullScreenStage.show();

                //System.err.println("TextArea in editor.fxml is null");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

The controller for the text area


package com.editor.xml_editor;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;

import java.net.URL;
import java.util.ResourceBundle;

public class editorController implements Initializable {
    @FXML
    protected TextArea inputText;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        this.inputText.setText("Hello World");
    }

    public void setInputText(String input) {
        inputText.setText(input);
    }

    // Assuming you want to expose this TextArea


    // This TextArea is used internally



}


Fxml for the text Area


<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/20" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.editor.xml_editor.editorController">
   <children>
     
      <BorderPane layoutY="26.0" prefHeight="370.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.600000000000023">
         
         <center>
            <SplitPane dividerPositions="0.48328877005347587" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TextArea id="inputText" layoutX="15.2" layoutY="48.0" prefHeight="322.0" prefWidth="290.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TextArea id="outputText" editable="false" layoutX="41.0" layoutY="53.0" prefHeight="322.0" prefWidth="290.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

I tried all the programming I could

1

There are 1 best solutions below

1
jewelsea On

The identity attribute is wrong in your FXML document.

You should be using fx:id, not id.

This:

<TextArea id="inputText" . . . 

Should be:

<TextArea fx:id="inputText" . . .

Similarly for your other ids defined in FXML like for outputText.

By setting id, you are only setting the CSS ID, not an FXML reference that can be injected by the loader into a controller which uses the @FXML annotation.

The FXML documentation isn't really good at documenting this topic, but it says:

Assigning an fx:id value to an element creates a variable in the document's namespace that can later be referred to by variable dereference attributes, such as the "toggleGroup" attribute shown above, or in script code, discussed in a later section. Additionally, if the object's type defines an "id" property, this value will also be passed to the objects setId() method.

So fx:id also sets id (which is used as a CSS identifier), in addition to putting a reference to the object in the namespace which can be extracted and set by the loader on public references in your controller class or references annotated with the @FXML annotation.

See the documentation on controllers and @FXML for more info and examples.