0

So recently I've been having problems with javafx. I was working on my Javafx program and I wanted to make my choiceBox items initialized as soon as the screen started, I set and populated an observable list and set it to the choicebox. I made a method called loadChoiceBoxList, and I called it in initializable, and this happened

Exception in Application start method
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 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: javafx.fxml.LoadException: 
/Users/anish/IdeaProjects/FBLA2020CompetitionCode/out/production/FBLA2020CompetitionCode/sample/HOME.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.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Caused by: java.lang.NullPointerException
    at sample.controllerHome.loadChoiceBox(controllerHome.java:49)
    at sample.controllerHome.initialize(controllerHome.java:35)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    ... 14 more
Exception running application sample.Main

Here is my source code for my controller

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.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class controllerHome implements Initializable {
    //Table DATA



    //Ok button for the instructions
    @FXML private Button okButton;

    //Edit box buttons/controls
    @FXML private Button okButtonEdit;
    @FXML private Button cancel;
    @FXML private ChoiceBox chooseStudent;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        loadChoiceBox();

    }
    private ObservableList gradelistChoiceBox = FXCollections.observableArrayList();

    private void loadChoiceBox(){
        gradelistChoiceBox.removeAll(gradelistChoiceBox);
        gradelistChoiceBox.add(6);
        gradelistChoiceBox.add(7);
        gradelistChoiceBox.add(8);
        gradelistChoiceBox.add(9);
        gradelistChoiceBox.add(10);
        gradelistChoiceBox.add(11);
        gradelistChoiceBox.add(12);
        gradeChoiceBox.getItems().addAll(gradelistChoiceBox);
    }


    public void loadHome(ActionEvent event) throws IOException {
        Parent view2 = FXMLLoader.load(getClass().getResource("HOME.fxml"));
        Scene scene2 = new Scene(view2);
        Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
        window.setScene(scene2);
        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"));
        Stage window = new Stage();
        Scene stage = new Scene(view2);
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Instructions");
        window.setScene(stage);
        window.show();
        System.out.println("System log: Loaded instructions");

    }

    public void editPopup(ActionEvent actionEvent) throws IOException{
        Parent view = FXMLLoader.load(getClass().getResource("editBox.fxml"));
        Stage window = new Stage();
        Scene stage = new Scene(view);
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Edit");
        window.setScene(stage);
        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"));
        Stage window = new Stage();
        Scene stage = new Scene(view);
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Add Student");
        window.setScene(stage);
        window.show();
    }

    public void deleteStudentPopup(ActionEvent actionEvent) throws IOException{
        Parent view = FXMLLoader.load(getClass().getResource("deleteStudent.fxml"));
        Stage window = new Stage();
        Scene stage = new Scene(view);
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Delete Student");
        window.setScene(stage);
        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 void exit(ActionEvent actionEvent){

    }

    public void editNameText(ActionEvent actionEvent) {
    }

    public void editButtons(ActionEvent actionEvent) {
    }

    @FXML private TextField addStudentTextBox;
    @FXML private ChoiceBox<Integer> gradeChoiceBox;
    @FXML private Button okButtonAS;
    @FXML private Button cancelAS;
    @FXML private Label errorAS;


    public boolean hasNumbers(TextField text){
        String str = text.getText();
        if (str == null || str.length() == 0) {
            return false;
        }

        try {

            Integer.parseInt(str);
            return true;

        } catch (NumberFormatException e) {
            return false;
        }
    }


    //all actions for addStudentButtons
    public void addStudentComponents(ActionEvent actionEvent) {
        if(actionEvent.getSource() == okButtonAS){
            if(addStudentTextBox.getText().equals(null) && gradeChoiceBox.getValue() == null){
                errorAS.setVisible(true);
                errorAS.setText("ERROR: The textbox or choicebox may be set to null!");

            }
            if(hasNumbers(addStudentTextBox)){
                errorAS.setVisible(true);
                errorAS.setText("ERROR: BRUH, stop playing wit me. Are you goddamn serious. NOBODY HAS NUMBERS IN THEIR NAME ALIEN ASS");
            }
        }


    }
}

0 Answers0