Im fairly new to both stackoverflow and javafx so please be nice.
Description of what Im doing:
I am making a simple quiz game. First window is just a like a welcome/start screen, when that button is clicked we get to the second screen where all the category buttons are, when one of them is clicked it will randomly pick a question of that kind of category user has selected and the third and last window will appear with the questions category as a label, question as textfield and answear as textfield.
Problem: Whenever a category is clicked I need that current controller to set the next controllers textfields and label. I havent come to far with this. I just get a nullpointerexception when im calling the setQuestion method in the second controller, FXMLCategoriesDocumentController, when I tried to debug it it just says that the instantiated "questControll" is null all the time, and the "questControll.question/category/answear" is referenced from a null object
Code:
second controller
public class FXMLCategoriesDocumentController implements Initializable {
/**
* Initializes the controller class.
*/
private FXMLQuestionDocumentController questControll;
private Question quest;
@FXML
private void geografButtonAction(ActionEvent event) {
try {
FXMLLoader fxmlQuestLoader = new FXMLLoader(getClass().getResource("FXMLQuestionDocument.fxml"));
this.questControll = fxmlQuestLoader.<FXMLQuestionDocumentController>getController();
quest = new Question("Geografi", "Vad heter Sveriges huvudstad?", "Stockholm");
questControll.setQuestion(quest.getCategory(), quest.getQuestion(), quest.getAnswear());
Parent root1 = (Parent) fxmlQuestLoader.load();
root1.setId("pane");
Stage app_stage = (Stage)((Node) event.getSource()).getScene().getWindow();
Scene root1_scene = new Scene(root1);
root1_scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
app_stage.hide();
app_stage.setScene(root1_scene);
app_stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
third controller
public class FXMLQuestionDocumentController implements Initializable {
private FXMLCategoriesDocumentController catControll;
private Question quest;
@FXML
public Label category = new Label();
@FXML
public TextField question = new TextField();
@FXML
public TextField answear = new TextField();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setQuestion(String cat, String quest, String ans){
if(category.getText() == null || question.getText() == null || answear.getText() == null){
System.out.println("everything is null");
}else{
category.setText(cat);
question.setText(quest);
answear.setText(ans);
}
}
Question class
public class Question {
private String category;
private String question;
private String answear;
public Question(String cat, String quest, String ans){
this.category = cat;
this.question = quest;
this.answear = ans;
}
public void setCategory(String cat){
this.category = cat;
}
public void setQuestion(String quest){
this.question = quest;
}
public void setAnswear(String ans){
this.answear = ans;
}
public String getCategory(){
return category;
}
public String getQuestion(){
return question;
}
public String getAnswear(){
return answear;
}
}
FXML second controller(category) category xml
FXML third controller(question)