i want to create a little quiz app with javafx, scenebuilder and sqlite. right now i struggle to give the created questions i wrote in sqlite over to my controller. i want to call the setQuestions method every time i start the quiz, so i made "ControllerQuiz controllerQuiz = fxmlLoader.getController();"
Here is my Category menu where i want to give the questions from SQLite over:
@FXML
private Button Computer;
@FXML
private Button Science;
@FXML
private Button Java;
@FXML
private Button Animal;
@FXML
private Button City;
@FXML
private Button Country;
@FXML
private Label Lable;
@FXML
private HBox statusHBox;
@FXML
private Circle dbstatuslight;
@FXML
private Button OK;
@FXML
private Button abort;
// --------- Arraylist --------
List<String> categoryList = new ArrayList<String>();
List<Question> quizQuestionList = new ArrayList<>();
Database database = new Database();
QuestionBank questionBank = new QuestionBank();
@FXML
void ComputerButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(),button);
}
@FXML
void ScienceButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(),button);
}
@FXML
void JavaButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(),button);
}
@FXML
void AnimalButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(),button);
}
@FXML
void CityButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(), button);
}
@FXML
void CountryButton(ActionEvent event) {
Button button = (Button) event.getSource();
getCategory(button.getText(), button);
}
//------- HBox --------
@FXML
void OKButton(ActionEvent event) {
for (String category: categoryList) {
questionBank.loadCategoryQuestions(database.getStatement(), category);
}
quizQuestionList = questionBank.getQuestionList();
for (Question question: quizQuestionList) {
System.out.println("ID: " + question.getQuestion_id());
}
Button okButton = (Button) event.getSource();
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
try {
startQuiz();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
void abortButton(ActionEvent event) {
Computer.setDisable(false);
Java.setDisable(false);
Science.setDisable(false);
City.setDisable(false);
Country.setDisable(false);
Animal.setDisable(false);
statusHBox.setDisable(true);
Lable.setText(" ");
}
@Override
public void initialize(URL location, ResourceBundle resources) {
statusHBox.setDisable(true);
//Database connection
boolean dbConnection = database.open();
if(dbConnection) {
dbstatuslight.setFill(Color.GREEN);
} else {
dbstatuslight.setFill(Color.RED);
}
}
//---------- methods ----------
private void getCategory(String category, Button button) {
categoryList.add(category);
Lable.setText(Lable.getText() + " " + category);
button.setDisable(true);
statusHBox.setDisable(false);
}
private void startQuiz() throws IOException {
Stage stage3 = new Stage();
stage3.setTitle("Quiz");
FXMLLoader fxmlLoader = new FXMLLoader();
AnchorPane root = (AnchorPane) fxmlLoader.load(new File("src/main/java/org/example/quizapp/controller/quiz.fxml").toURI().toURL());
//AnchorPane root = (AnchorPane) fxmlLoader.load(getClass().getClassLoader().getResource("/main/java/org/example/quizapp/controller/quiz.fxml").openStream());
ControllerQuiz controllerQuiz = fxmlLoader.getController();
controllerQuiz.setQuestions((ArrayList<Question>) quizQuestionList);
Scene scene3 = new Scene(root);
stage3.setScene(scene3);
stage3.setResizable(false);
stage3.show();
}
}
my Controller for that category menu:
@FXML
private Label Question;
@FXML
private Button YES;
@FXML
private Button NO;
@FXML
private Label questionCounter;
@FXML
private Label score;
@FXML
private Label Highscore;
private ArrayList<Question> questionList = new ArrayList<>();
@FXML
void answerButton(ActionEvent event) {
}
//methods
public void setQuestions(ArrayList<org.example.quizapp.menu.questions.Question> qList) {
questionList = qList;
for (Question question: questionList) {
System.out.println(question.getQuestion_id());
}
}
}
the question bank:
private List<Question> questionList = new ArrayList<>();
public void loadCategoryQuestions (Statement statement, String categoryTable) {
String QUERY_DATA_FROM_TABLE = "SELECT * FROM " + categoryTable;
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery(QUERY_DATA_FROM_TABLE);
while (resultSet.next()) {
int question_id = resultSet.getInt(1);
String question_text = resultSet.getString(2);
boolean question_answer;
if (resultSet.getInt(3) == 1) {
question_answer = true;
} else {
question_answer = false;
}
String question_complement = resultSet.getString(4);
Question question = new Question(question_id, question_text, question_answer, question_complement);
questionList.add(question);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Question> getQuestionList() {
return questionList;
}
public void setQuestionList(List<Question> questionList) {
this.questionList = questionList;
}
}
the error log i get when i try to compile:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8885)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782) ... 44 more Caused by: java.lang.NullPointerException at quizapp/org.example.quizapp.menu.category.CategoryMenu.startQuiz(CategoryMenu.java:178) at quizapp/org.example.quizapp.menu.category.CategoryMenu.OKButton(CategoryMenu.java:127) ... 55 more
Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782) ... 44 more Caused by: java.lang.NullPointerException at quizapp/org.example.quizapp.menu.category.CategoryMenu.startQuiz(CategoryMenu.java:178) at quizapp/org.example.quizapp.menu.category.CategoryMenu.OKButton(CategoryMenu.java:127)