字符串数组类型变量的值不会更新(从另一个类引用时返回空)。 JavaFX的

时间:2014-01-06 10:40:42

标签: java nullpointerexception javafx-2

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

FirstController.java(String [] strArray在这里声明并初始化)

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(“另一个类”,其中String [] strArray在打印时显示为空)

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(应用程序类):

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 个答案:

答案 0 :(得分:2)

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

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?
  }
}

现在在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();  
}