T
T
Thymomenos Gata2017-10-14 18:46:05
Avocode
Thymomenos Gata, 2017-10-14 18:46:05

Why doesn't the window controller see the list from the main class?

Generally. Such situation.
I pass from the main class to the class-controller of the settings window a list with the values ​​​​set earlier in the main class through the constructor. Here is the main class code:

public class MainClass extends Application {
  
  private Stage primaryStage;
  private BorderPane rootLayout;
  private Settings settings;
  
  
  @Override
  public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Антикафе");
    
    initRootLayout();
    showPersonOverview();
  }
  
   private ObservableList<Settings> Data = FXCollections.observableArrayList();
  
  public MainClass() {
    Data.add(new Settings(false,false,false,false));
  }
  
  public ObservableList<Settings> getData() {
        return Data;
    }
   public boolean showSettingsDialog() {
        try {
            // Загружаем fxml-файл и создаём новую сцену
            // для всплывающего диалогового окна.
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainClass.class.getResource("view/settings.fxml"));
            AnchorPane page = (AnchorPane) loader.load();

            // Создаём диалоговое окно Stage.
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Настройки");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            dialogStage.initOwner(primaryStage);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);
            
            
         // Передаём список настроек в контроллер.
            ButtonSettingsControll controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setData(Data);
            controller = new ButtonSettingsControll(Data);
            
            // Отображаем диалоговое окно и ждём, пока пользователь его не закроет
            dialogStage.showAndWait();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

Here is the code for the controller itself:
public class ButtonSettingsControll {
  
    private Stage dialogStage;
    @FXML
    public CheckBox checkKal;
    @FXML
    public CheckBox checkStopCheck;
    @FXML
    public CheckBox checkTea;
    @FXML
    public CheckBox checkVIP;
    @FXML
    public TextField kalHard;
    @FXML
    public TextField kalNorm;
    @FXML
    public TextField kalLight;
    @FXML
    public TextField priceTea;
    @FXML
    public TextField priceUnd;
    @FXML
    public TextField peoplsUndo;
    @FXML
    public TextField priceTo;
    @FXML
    public TextField peoplsTo;
    @FXML
    public TextField priceStopCheck;
    
    private MainClass mainClass;
    private ObservableList<Settings> Data = FXCollections.observableArrayList();
    
    public void setMain(MainClass mainClass) {
      this.mainClass = mainClass;
    }
    public void setData(ObservableList<Settings> data) {
      Data = data;
    }
    
                //Пока не разберусь со списком для наглядности буду использовать это
    Boolean CK = false;
    Boolean CT = false;
    Boolean CV = false;
    Boolean CC = false;
    
    public void setDialogStage(Stage dialogStage) {
          this.dialogStage = dialogStage;
      }
    
    public ButtonSettingsControll() {
    }
    public ButtonSettingsControll(ObservableList<Settings> data) {
      Data = data;
      System.out.println(Data.get(0).getCheckK());
      checkKal.setSelected(data.get(0).getCheckK());
      
    }
    
    @FXML
      private void initialize() {
      if(CK)
        checkKal.setSelected(true);
      else {
        checkKal.setSelected(false);
        kalHard.setDisable(true);
        kalNorm.setDisable(true);
        kalLight.setDisable(true);
      }
      if(CT)
        checkTea.setSelected(true);
      else {
        checkTea.setSelected(false);
        priceTea.setDisable(true);
      }
      if(CV)
        checkVIP.setSelected(true);
      else {
        checkVIP.setSelected(false);
        priceUnd.setDisable(true);
        priceTo.setDisable(true);
        peoplsTo.setDisable(true);
        peoplsUndo.setDisable(true);
      }
      if(CC)
        checkStopCheck.setSelected(true);
      else {
        checkStopCheck.setSelected(false);
        priceStopCheck.setDisable(true);
      }
    }
    
    @FXML
    private void settings() {
      Data.setAll(new Settings(checkKal.isSelected(),checkTea.isSelected(),checkVIP.isSelected(),checkStopCheck.isSelected()));
      System.out.println(Data.get(0).getCheckK());
          dialogStage.close();
    }
}

I can't figure out why I'm getting an exception (java.lang.NullPointerException) when I call the settings window.
The exception appears in this block:
public ButtonSettingsControll(ObservableList<Settings> data) {
      Data = data;
      System.out.println(Data.get(0).getCheckK());
      checkKal.setSelected(Data.get(0).getCheckK());
      
    }

Namely here
checkKal.setSelected(data.get(0).getCheckK());
.
The output to the console works, (done for verification), but when I try to use the same value to set the checkbox in the checkbox, the program crashes due to this exception. Plz tell me what is wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Korobov, 2017-10-20
@prumin

here is an error.

// Передаём список настроек в контроллер.
            ButtonSettingsControll controller = loader.getController();
            controller.setDialogStage(dialogStage);
            controller.setData(Data);
            controller = new ButtonSettingsControll(Data);

here
ButtonSettingsControll controller = loader.getController();
received a link to the controller from the loader.
and here controller = new ButtonSettingsControll(Data);created a new object not related to the loaded FXML
And more. When the controller's constructor is called, the controls declared with the @FXML annotation have not yet been created. Therefore, such a constructor does not make sense. And the loader calls the default constructor (the one without parameters). This means that either after receiving a link from the loader to the controller, you call its setData method and anything else and fill in the data controls. Or you create a controller and pass a reference to it to the loader and then call the controller method in which you fill your controls with data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question