字符串数组类型变量';s值不为';t get updated(从另一个类引用时返回空)。JavaFX

字符串数组类型变量';s值不为';t get updated(从另一个类引用时返回空)。JavaFX,java,nullpointerexception,javafx-2,Java,Nullpointerexception,Javafx 2,我在这里遗漏了一些基本的东西。我有一个字符串数组(string[]strArray),它在一个类(FirstController.java)中声明,然后由一个方法初始化。我试图从另一个类(SecondController.java)打印此数组,但返回的数组为空。请让我明白为什么会这样。非常感谢。请参阅以下代码(由James_D提供): FirstController.java(此处声明并初始化字符串[]strArray) First.java(应用程序类): 所以看起来SecondControl

我在这里遗漏了一些基本的东西。我有一个字符串数组(string[]strArray),它在一个类(FirstController.java)中声明,然后由一个方法初始化。我试图从另一个类(SecondController.java)打印此数组,但返回的数组为空。请让我明白为什么会这样。非常感谢。请参阅以下代码(由James_D提供):

FirstController.java(此处声明并初始化字符串[]strArray)

First.java(应用程序类):


所以看起来SecondController需要访问某种从别处填充的字符串数组。只需在SecondController中定义一个ObservableList并给它一个get(…)方法

公共类控制器{
private StringProperty text=新的SimpleStringProperty(此“文本”为“”);
//删除对FirstController的任何引用
//FirstController FirstController=新的FirstController();
私有ObservableList strArray=FXCollections.observableArrayList();
@FXML
私人文本区域文本区域2;
公共字符串属性textProperty(){
返回文本;
}
公共最终字符串getText2(){
返回text.get();
}
公共最终void setText(字符串文本){
this.text.set(text);
}
公共观察专家getStrArray(){
回程线;
}
// ...
@FXML
私有void showTextAction(ActionEvent事件){
set(textArea2.getText());
System.out.println(firstController.strArray);//此行打印空数组[]
System.out.println(firstController.strArray.toString());//这将导致NullPointerException,因为数组是空的。但是为什么它是空的呢?
}
}
现在在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();  
}
@FXML
私有void openStage2Action(ActionEvent事件)引发IOException{
阶段2=新阶段();
FXMLLoader FXMLLoader=新的FXMLLoader(getClass().getResource(“Second.fxml”);
对象根=fxmlLoader.load();
场景=新场景((父)根);
第二阶段:场景;
SecondController SecondController=(SecondController)fxmlLoader.getController();
secondController.textProperty().addListener(新的ChangeListener()){
@凌驾

public void changed(ObservalEvalue)非常感谢!据我所知,最好处理可观察属性(而不是实例化类或UI元素)。您的解决方案有效。是的:您不想将您的“内部”控制器到任何特定的上下文,因为这会降低可重用性的机会。并且您不想公开UI组件:您可能希望稍后更改UI,例如用组合框替换文本字段,或者其他什么。如果其他类依赖于特定的设计,这将变得不可能。通常,请考虑类n从那里学习和工作。
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?
  }
} 
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);}
}
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?
  }
}
@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();  
}