0
votes

I am missing something fundamental here. I have a string array (String[] strArray) that is declared in a class (FirstController.java), and then initialized by a method. I am trying to print this array from another class (SecondController.java), but the array returned is empty. Please make me understand why this is happening. Thank you! Please see the following code (courtesy James_D):

FirstController.java (String[] strArray is declared and initialized here)

public class FirstController {

    private Stage Stage2;
    public String[] strArray; // this is the string array in question. 

    @FXML
    private Button openStage2;
    @FXML
    private TextArea textArea1;

    @FXML
    private void openStage2Action(ActionEvent event) throws IOException {
        Stage2 = new Stage();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
        Object root = fxmlLoader.load();
        Scene scene = new Scene((Parent) root);
        Stage2.setScene(scene);
        SecondController secondController = (SecondController)fxmlLoader.getController();
        secondController.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
                textArea1.setText(newValue);
                strArray = newValue.split("\n"); // here the string array is initialized.
                // this string array gets printed by this
                for(String s:strArray){
                    System.out.println(s);
                }   
            } 
        });
        Stage2.show();  
    }

    //public String[] getStrArray(){
    //    return strArray;
    //}

}

SecondController.java ("another class" from where the String[] strArray results as empty, when printed)

public class SecondController {
  private StringProperty text  = new SimpleStringProperty(this, "text", "");;
  FirstController firstController = new FirstController();

  @FXML
  private TextArea textArea2 ;
  public StringProperty textProperty() {
    return text ;
  }
  public final String getText2() {
    return text.get();
  }
  public final void setText(String text) {
    this.text.set(text);
  }

  // ...

  @FXML 
  private void showTextAction(ActionEvent event) {
    text.set(textArea2.getText()); 
    System.out.println(firstController.strArray); // this line prints empty array []

    System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty?
  }
} 

First.java (Application class):

public class First extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("First.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {launch(args);}
}
1

1 Answers

2
votes

So it looks like SecondController needs to access some kind of string array that is populated from elsewhere. Just define an ObservableList in SecondController and give it a get(...) method.

public class SecondController {
  private StringProperty text  = new SimpleStringProperty(this, "text", "");
  // Remove any reference to FirstController
  //FirstController firstController = new FirstController();
  private ObservableList<String> strArray = FXCollections.observableArrayList();

  @FXML
  private TextArea textArea2 ;
  public StringProperty textProperty() {
    return text ;
  }
  public final String getText2() {
    return text.get();
  }
  public final void setText(String text) {
    this.text.set(text);
  }
  public ObservableList<String> getStrArray() {
    return strArray ;
  }

  // ...

  @FXML 
  private void showTextAction(ActionEvent event) {
    text.set(textArea2.getText()); 
    System.out.println(firstController.strArray); // this line prints empty array []

    System.out.println(firstController.strArray.toString()); // this will result NullPointerException because the array is empty. But why it is empty?
  }
}

and now in FirstController:

@FXML
private void openStage2Action(ActionEvent event) throws IOException {
    Stage2 = new Stage();

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second.fxml"));
    Object root = fxmlLoader.load();
    Scene scene = new Scene((Parent) root);
    Stage2.setScene(scene);
    SecondController secondController = (SecondController)fxmlLoader.getController();
    secondController.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
            textArea1.setText(newValue);
            strArray = newValue.split("\n"); // here the string array is initialized.
            // this string array gets printed by this
            for(String s:strArray){
                System.out.println(s);
            }   
        } 
    });
    secondController.getStrArray().setAll(...); // or addAll(...) as needed.
    Stage2.show();  
}