有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

第一控制器。java(这里声明并初始化字符串[]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;
    //}

}

副控制员。java(“另一个类”,其中字符串[]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?
  }
} 

首先。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) 个答案

  1. # 1 楼答案

    看起来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();  
    }