I have a project in JavaFX in which it tracks student's volunteer records. I am having problems with the initialize method. I called a Combobox to fill an observable ArrayList when it initializes. When it initializes, no errors are thrown, but when I click a button, I get this weird error message:
11
System log: Launched
11
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:238)
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:8890)
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:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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:3862)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
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:567)
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:567)
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: javafx.fxml.LoadException:
/Users/anish/IdeaProjects/FBLA2020CompetitionCode/out/production/FBLA2020CompetitionCode/sample/addStudent.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at sample.controllerHome.addStudentPopup(controllerHome.java:115)
... 55 more
Caused by: java.lang.NullPointerException
at sample.controllerHome.initialize(controllerHome.java:68)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
... 63 more
Process finished with exit code 0
here is the controller class
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.regex.Pattern;
public class controllerHome implements Initializable {
//csv class
csvReader reader;
//Global Screen, keeps track of the screens
Scene scene;
Stage window;
//global components needed
@FXML private ComboBox<String> chooseStudent;
//hashmap and the student
HashMap studentMap = new HashMap();
ArrayList<volunteer> volunteerHours;
ObservableList<String> allNames = csvReader.getName();
//Table DATA
// add student components
@FXML private TextField addStudentTextBox;
@FXML private Button okButtonAS;
@FXML private Button cancelAS;
@FXML private Label errorAS;
@FXML private TextField gradeTextBoxAS;
//Ok button for the instructions
@FXML private Button okButton;
//Edit box buttons/controls
@FXML private Button okButtonEdit;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
chooseStudent.setItems(allNames);
}
public void loadHome(ActionEvent event) throws IOException {
Parent view2 = FXMLLoader.load(getClass().getResource("HOME.fxml"));
scene= new Scene(view2);
window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
System.out.println("System log: Loaded home");
}
public void print(ActionEvent actionEvent) throws IOException{
}
public void loadInstructions(ActionEvent actionEvent) throws IOException{
Parent view2 = FXMLLoader.load(getClass().getResource("instructions.fxml"));
window = new Stage();
scene = new Scene(view2);
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Instructions");
window.setScene(scene);
window.show();
System.out.println("System log: Loaded instructions");
}
public void editPopup(ActionEvent actionEvent) throws IOException{
Parent view = FXMLLoader.load(getClass().getResource("editBox.fxml"));
window = new Stage();
scene = new Scene(view);
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Edit");
window.setScene(scene);
window.show();
System.out.println("System log: Loaded edit");
}
public void searchStudent(ActionEvent actionEvent) {
}
public void addStudentPopup(ActionEvent actionEvent) throws IOException{
Parent view = FXMLLoader.load(getClass().getResource("addStudent.fxml"));
window = new Stage();
scene = new Scene(view);
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Add Student");
window.setScene(scene);
window.show();
}
public void deleteStudentPopup(ActionEvent actionEvent) throws IOException{
Parent view = FXMLLoader.load(getClass().getResource("deleteStudent.fxml"));
Stage window = new Stage();
scene = new Scene(view);
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle("Delete Student");
window.setScene(scene);
window.show();
}
public void loadStudent(ActionEvent actionEvent) throws IOException{
Parent view2 = FXMLLoader.load(getClass().getResource("student.fxml"));
Scene scene2 = new Scene(view2);
Stage window = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
window.setScene(scene2);
window.show();
}
public boolean stringContainsNumber( String s )
{
return Pattern.compile( "[0-9]" ).matcher( s ).find();
}
//all actions for addStudentButtons
public void addStudentComponents(ActionEvent actionEvent) throws IOException{
if(actionEvent.getSource() == okButtonAS) {
System.out.println("inside ok button");
if (!addStudentTextBox.getText().equals("") && !gradeTextBoxAS.equals("")) {
System.out.println("passed the null checker");
boolean x = false;
try {
Integer.parseInt(gradeTextBoxAS.getText());
x = true;
} catch (NumberFormatException e) {
x = false;
}
if(x == true && (Integer.parseInt(gradeTextBoxAS.getText()) > 12 || Integer.parseInt(gradeTextBoxAS.getText()) < 6)){
System.out.println("did not pass the grade checker");
errorAS.setText("ERROR: You entered a grade < 6 or > 12 ");
errorAS.setVisible(true);
}
else{
System.out.println("passed grade checker");
if(stringContainsNumber(addStudentTextBox.getText()) || x == false){
System.out.println("Found compatibility error");
errorAS.setVisible(true);
errorAS.setText("ERROR: compatibility error!");
}
else{
csvReader.addStudentCSV(addStudentTextBox.getText(),Integer.parseInt(gradeTextBoxAS.getText()),getRandomNumberInRange(11111111,99999999));
allNames.add(addStudentTextBox.getText());
}
}
}
else {
System.out.println("didnt pass null checker");
errorAS.setVisible(true);
errorAS.setText("text fields are null");
}
}
if(actionEvent.getSource() == cancelAS){
Stage window = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
window.close();
}
}
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
public void editPopupComponents(ActionEvent actionEvent) {
}
}
Here is the code to HOME.FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="homePane" style="-fx-background-color: #02030A;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.controllerHome">
<children>
<VBox alignment="TOP_CENTER" style="-fx-background-color: #3282B8#3282B8;" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<ImageView fitHeight="210.0" fitWidth="227.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/Flag-color.png" />
</image>
<VBox.margin>
<Insets bottom="10.0" top="50.0" />
</VBox.margin></ImageView>
<Label text="FBLA Volunteer Portal" textFill="WHITE">
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<Button fx:id="homeButton" alignment="BASELINE_LEFT" graphicTextGap="22.0" mnemonicParsing="false" onAction="#loadHome" prefHeight="210.0" prefWidth="256.0" style="-fx-background-color: #3282B8#3282B8;" text="Home" textFill="WHITE">
<graphic>
<ImageView fitHeight="23.0" fitWidth="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/icons8_Home_32px.png" />
</image>
</ImageView>
</graphic>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets />
</VBox.margin>
<padding>
<Insets left="50.0" />
</padding>
</Button>
<Button fx:id="printButton" alignment="BASELINE_LEFT" graphicTextGap="22.0" mnemonicParsing="false" onAction="#print" prefHeight="210.0" prefWidth="256.0" style="-fx-background-color: #3282B8#3282B8;" text="Print" textFill="WHITE">
<graphic>
<ImageView fitHeight="26.0" fitWidth="28.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/icons8_Open_Envelope_32px.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="50.0" />
</padding>
</Button>
</children>
</VBox>
<StackPane layoutX="258.0" style="-fx-background-color: #02030A;">
<children>
<Pane fx:id="exit" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="685.0" prefWidth="790.0" style="-fx-background-color: #1b262c#1b262c;">
<children>
<TextField fx:id="searchStudent" layoutX="590.0" layoutY="47.0" onAction="#searchStudent" prefHeight="27.0" prefWidth="183.0" promptText="Search Students" style="-fx-background-color: #1b262c#1b262c; -fx-border-color: #B7C3D7; -fx-border-radius: 2em;" />
<Label layoutX="26.0" layoutY="25.0" prefHeight="29.0" prefWidth="404.0" text="FBLA/PBL Volunteer Tracker " textFill="#e7e5e5">
<font>
<Font size="24.0" />
</font>
</Label>
<HBox alignment="CENTER_LEFT" layoutX="26.0" layoutY="93.0" prefHeight="120.0" prefWidth="746.0" spacing="50.0">
<children>
<VBox alignment="CENTER" prefHeight="120.0" prefWidth="187.0">
<children>
<Label fx:id="totalHours" text="N/A" textFill="#2a73ff">
<font>
<Font name="System Bold" size="26.0" />
</font>
</Label>
<Label prefHeight="20.0" prefWidth="81.0" text=" Total Hours" textFill="#e7e5e5" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="120.0" prefWidth="187.0">
<children>
<Label fx:id="monthlyHours" text="N/A" textFill="#26bfbc">
<font>
<Font name="System Bold" size="26.0" />
</font>
</Label>
<Label text="Monthly Hours" textFill="#e7e5e5" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="120.0" prefWidth="187.0">
<children>
<Label fx:id="schoolYearHours" text="N/A" textFill="#fa5f7e">
<font>
<Font name="System Bold" size="26.0" />
</font>
</Label>
<Label prefHeight="20.0" prefWidth="100.0" text="School Year Hours" textFill="#e7e5e5" textOverrun="CLIP" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="120.0" prefWidth="187.0">
<children>
<Label fx:id="studentCount" text="N/A" textFill="#de0000">
<font>
<Font name="System Bold" size="26.0" />
</font>
</Label>
<Label prefWidth="100.0" text=" # of Students" textFill="#e7e5e5" textOverrun="CLIP" />
</children>
</VBox>
</children>
</HBox>
<ScrollPane layoutX="22.0" layoutY="283.0" prefHeight="385.0" prefWidth="746.0">
<content>
<VBox prefHeight="385.0" prefWidth="744.0" spacing="5.0" style="-fx-background-color: #02030A;">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<children>
<TableView fx:id="tableView" prefHeight="392.0" prefWidth="734.0" style="-fx-background-color: #1b262c#1b262c #1b262c#1b262c;">
<columns>
<TableColumn fx:id="Name" prefWidth="75.0" text="Name" />
<TableColumn fx:id="Grade" prefWidth="75.0" text="Grade" />
<TableColumn fx:id="ID" prefWidth="75.0" text="ID" />
<TableColumn fx:id="totalHours" prefWidth="75.0" text="totalHours" />
<TableColumn fx:id="Awards" prefWidth="75.0" text="Awards" />
<TableColumn fx:id="hours" prefWidth="75.0" text="Hours" />
<TableColumn fx:id="program" prefWidth="75.0" text="Program" />
<TableColumn fx:id="date" prefWidth="75.0" text="Date" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</VBox>
</content>
</ScrollPane>
<ImageView fitHeight="17.0" fitWidth="16.0" layoutX="749.0" layoutY="53.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/icons8_Search_52px.png" />
</image>
</ImageView>
<HBox alignment="CENTER_LEFT" layoutX="22.0" layoutY="213.0" prefHeight="49.0" prefWidth="746.0" spacing="50.0">
<children>
<VBox alignment="CENTER" prefHeight="70.0" prefWidth="187.0">
<children>
<Button fx:id="instructionButton" mnemonicParsing="false" onAction="#loadInstructions" prefHeight="27.0" prefWidth="185.0" style="-fx-background-color: #2a73ff#2a73ff;" text="Instructions" textFill="WHITE" />
<Label textFill="#e7e5e5" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="70.0" prefWidth="187.0">
<children>
<Button fx:id="editButton" mnemonicParsing="false" onAction="#editPopup" prefHeight="27.0" prefWidth="165.0" style="-fx-background-color: #26bfbc#26bfbc;" text="Edit" textFill="WHITE" />
<Label prefHeight="0.0" prefWidth="71.0" textFill="#e7e5e5" textOverrun="CLIP" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="70.0" prefWidth="187.0">
<children>
<Button fx:id="addStudentButton" mnemonicParsing="false" onAction="#addStudentPopup" prefHeight="27.0" prefWidth="290.0" style="-fx-background-color: #fa5f7e#fa5f7e;" text="Add Student" textFill="WHITE" textOverrun="CLIP" />
<Label prefHeight="0.0" prefWidth="97.0" textFill="#e7e5e5" textOverrun="CLIP" />
<Button mnemonicParsing="false" prefHeight="27.0" prefWidth="290.0" style="-fx-background-color: #fa5f7e#fa5f7e;" text="Add Hours" textFill="WHITE" textOverrun="CLIP" />
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="70.0" prefWidth="187.0">
<children>
<Button fx:id="deleteStudentButton" mnemonicParsing="false" onAction="#deleteStudentPopup" prefHeight="27.0" prefWidth="196.0" style="-fx-background-color: #de0000#de0000;" text="Delete Student" textFill="WHITE" />
<Label prefHeight="0.0" prefWidth="98.0" textFill="#e7e5e5" textOverrun="CLIP" />
<Button mnemonicParsing="false" prefHeight="27.0" prefWidth="196.0" style="-fx-background-color: #de0000#de0000;" text="Delete Hours" textFill="WHITE" />
</children>
</VBox>
</children>
</HBox>
<Label layoutX="26.0" layoutY="47.0" prefHeight="29.0" prefWidth="404.0" text="Summary/Overview" textFill="#9e9a9a">
<font>
<Font size="18.0" />
</font>
</Label>
<ComboBox fx:id="chooseStudent" layoutX="430.0" layoutY="48.0" prefWidth="150.0" />
</children>
</Pane>
</children>
</StackPane>
</children>
<effect>
<DropShadow color="#1b1eeb" />
</effect>
</AnchorPane>
I think there is an error with the buttons itself in my controller class, its either I need to add try and catches or something is going on with the controller class. My code is a bit messy but I hope its readable, thanks!