-3

So guys I have been developing a JavaFx application that allows a user to easily add or remove entities from an H2 database. Now am trying to create a separate scene that will allow the user to enter the details of the entity each time the user clicks on the add button. But each time I try to populate the comboboxes and choiceboxes I get an exception but if I don't try to populate the boxes there is no exception.

Here is the method creating the separate scene

    /**
     * Method loads a new window when the add or edit button is clicked
     */
    private void loadAddOrEditWindow() throws IOException {
        Stage stage = new Stage();
        FXMLLoader editUi = new FXMLLoader(mainUIController.class.getResource(PATH_TO_EDIT_UI));
        Scene editScene = new Scene(editUi.load());
        Image icon = new Image(PATH_TO_ICON);
        stage.getIcons().add(icon);
        stage.setTitle("JFB Students Manager");
        stage.setScene(editScene);
        stage.centerOnScreen();
        stage.setResizable(false);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    }

The button click that is calling the method

    /**
     * Method adds a single entity to the database and then refreshes the tableview
     */
    public void addDataToDB() {
        try {
            loadAddOrEditWindow();
        } catch (IOException e) {
            ErrorHandler.failedToAddOrEditError();
        }
        System.out.println("Add to Database");
    }

The separate controller for the new window

public class editUIController implements Initializable{

    @FXML
    public TextField firstNameField;

    @FXML
    public TextField lastNameField;

    @FXML
    public ChoiceBox<Gender> genderSelector;

    @FXML
    public DatePicker dateOFBirthSelector;

    @FXML
    public TextField phoneNumberField;

    @FXML
    public TextField emailField;

    @FXML
    public ComboBox<Nation> nationalitySelector;

    @FXML
    public ComboBox<CountryCodes> countryCodeSelector;


    public void saveStudent() {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        nationalitySelector.setItems(FXCollections.observableList(List.of(Nation.values())));
        genderSelector.setItems(FXCollections.observableList(List.of(Gender.values())));
        countryCodeSelector.setItems(FXCollections.observableList(List.of(CountryCodes.values())));
    }

}

and finally the stack trace

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base@18.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base@18.0.1/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@18.0.1/javafx.scene.Node.fireEvent(Node.java:8797)
    at javafx.controls@18.0.1/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls@18.0.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls@18.0.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base@18.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base@18.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base@18.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base@18.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base@18.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base@18.0.1/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics@18.0.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
    at javafx.graphics@18.0.1/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
    at javafx.graphics@18.0.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics@18.0.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
    at javafx.graphics@18.0.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
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:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    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:568)
    at javafx.base@18.0.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml@18.0.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1854)
    ... 46 more
Caused by: java.lang.NullPointerException
    at java.base/java.util.ImmutableCollections$ListN.indexOf(ImmutableCollections.java:716)
    at javafx.base@18.0.1/com.sun.javafx.collections.ObservableListWrapper.indexOf(ObservableListWrapper.java:124)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.getIndexOfComboBoxValueInItemsList(ComboBoxListViewSkin.java:478)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.updateValue(ComboBoxListViewSkin.java:416)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.<init>(ComboBoxListViewSkin.java:169)
    at javafx.controls@18.0.1/javafx.scene.control.ComboBox.createDefaultSkin(ComboBox.java:447)
    at javafx.controls@18.0.1/javafx.scene.control.Control.doProcessCSS(Control.java:899)
    at javafx.controls@18.0.1/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls@18.0.1/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics@18.0.1/javafx.scene.Parent.doProcessCSS(Parent.java:1400)
    at javafx.graphics@18.0.1/javafx.scene.Parent$1.doProcessCSS(Parent.java:125)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.ParentHelper.processCSSImpl(ParentHelper.java:98)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics@18.0.1/javafx.scene.Parent.doProcessCSS(Parent.java:1400)
    at javafx.graphics@18.0.1/javafx.scene.Parent$1.doProcessCSS(Parent.java:125)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.ParentHelper.processCSSImpl(ParentHelper.java:98)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics@18.0.1/javafx.scene.Node.processCSS(Node.java:9477)
    at javafx.graphics@18.0.1/javafx.scene.Scene.doCSSPass(Scene.java:572)
    at javafx.graphics@18.0.1/javafx.scene.Scene.preferredSize(Scene.java:1770)
    at javafx.graphics@18.0.1/javafx.scene.Scene$2.preferredSize(Scene.java:396)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
    at javafx.graphics@18.0.1/javafx.stage.Window$12.invalidated(Window.java:1163)
    at javafx.base@18.0.1/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
    at javafx.base@18.0.1/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.graphics@18.0.1/javafx.stage.Window.setShowing(Window.java:1239)
    at javafx.graphics@18.0.1/javafx.stage.Window.show(Window.java:1254)
    at javafx.graphics@18.0.1/javafx.stage.Stage.show(Stage.java:277)
    at com.jfbonline.students.model.controllers.mainUIController.loadAddOrEditWindow(mainUIController.java:246)
    at com.jfbonline.students.model.controllers.mainUIController.addDataToDB(mainUIController.java:127)
    ... 57 more
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at java.base/java.util.ImmutableCollections$ListN.indexOf(ImmutableCollections.java:716)
    at javafx.base@18.0.1/com.sun.javafx.collections.ObservableListWrapper.indexOf(ObservableListWrapper.java:124)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.getIndexOfComboBoxValueInItemsList(ComboBoxListViewSkin.java:478)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.updateValue(ComboBoxListViewSkin.java:416)
    at javafx.controls@18.0.1/javafx.scene.control.skin.ComboBoxListViewSkin.<init>(ComboBoxListViewSkin.java:169)
    at javafx.controls@18.0.1/javafx.scene.control.ComboBox.createDefaultSkin(ComboBox.java:447)
    at javafx.controls@18.0.1/javafx.scene.control.Control.doProcessCSS(Control.java:899)
    at javafx.controls@18.0.1/javafx.scene.control.Control$1.doProcessCSS(Control.java:89)
    at javafx.controls@18.0.1/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(ControlHelper.java:67)
    at javafx.graphics@18.0.1/com.sun.javafx.scene.NodeHelper.processCSS(NodeHelper.java:146)
    at javafx.graphics@18.0.1/javafx.scene.Node.processCSS(Node.java:9477)
    at javafx.graphics@18.0.1/javafx.scene.Node.processCSS(Node.java:9470)
    at javafx.graphics@18.0.1/javafx.scene.Node.processCSS(Node.java:9470)
    at javafx.graphics@18.0.1/javafx.scene.Scene.doCSSPass(Scene.java:572)
    at javafx.graphics@18.0.1/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2494)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:405)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:404)
Caused by: java.lang.reflect.InvocationTargetException

    at javafx.graphics@18.0.1/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:434)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:575)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:555)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:548)
    at javafx.graphics@18.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:352)
    at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException


> Task :mainApplication.main() FAILED

Execution failed for task ':mainApplication.main()'.
> Build cancelled while executing task ':mainApplication.main()'

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

I tried to reproduce the error using a simpler example but I don't get the error but the example shows what am trying to get too;

package com.example.exampleerror;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;

public class HelloApplication extends Application {

    public enum Gender {
        M,
        F,
        O
    }

    @Override
    public void start(Stage stage) throws IOException {
        Pane first = new AnchorPane();
        Button openSecond = new Button(" Open Second ");
        openSecond.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                try {
                    loadAddOrEditWindow();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        first.getChildren().add(openSecond);
        Scene scene = new Scene(first, 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    private void loadAddOrEditWindow() throws IOException {
        Stage stage = new Stage();
        Pane pane = new AnchorPane();
        Button save = new Button("Save");
        pane.getChildren().add(save);

        Scene editScene = new Scene(pane, 320, 240);
        ChoiceBox<Gender> genders = new ChoiceBox<>();
        genders.setLayoutX(70.0);
        genders.setLayoutY(70.0);


        genders.setItems(FXCollections.observableList(List.of(Gender.values())));
        pane.getChildren().add(genders);
        stage.setTitle("Example");
        stage.setScene(editScene);
        stage.centerOnScreen();
        stage.setResizable(false);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

I just want to populate the choicebox using the initialize method each time the button is clicked but an error is occurring.

  • Add the full stack trace formatted as code. Provide a [mcve]. – jewelsea May 31 '22 at 08:03
  • Two questions: **(1)** Is there more to the stack trace, such as more `at ...` lines after the final `Caused by: java.lang.NullPointerException`? **(2)** Are you utilizing background threads and, if so, are you ensuring that only the _JavaFX Application Thread_ touches the active scene graph (directly or indirectly)? – Slaw May 31 '22 at 08:41
  • There is not more to the stack trace, I also tried to look for it, but nothing. Am not sure about background Threads but I haven't set up a thread or anything like that. – Alinaswe klb May 31 '22 at 09:00
  • Try [`observableArrayList(Nation.values())`](https://openjfx.io/javadoc/17/javafx.base/javafx/collections/FXCollections.html#observableArrayList(E...)), so the observable isn’t backed by an immutable list. I don’t know if that will help. – jewelsea May 31 '22 at 09:06
  • Unfortunately the stack trace is not helpful in this case. A [mcve] would be helpful so that this can be reproduced. Please read the link. Try creating a very small app, with no fxml, that populates a single combobox from an enumeration. Make sure you can do that. – jewelsea May 31 '22 at 09:07
  • @jewelsea can't show you the application while its running via a google meet call or something so that I can explain in more detail – Alinaswe klb May 31 '22 at 09:18
  • No, then the question and answer would not help anybody else. – jewelsea May 31 '22 at 09:19
  • I know, its way easier to explain. The main problem am facing is that I am trying to populate some checkboxes each time a new scene is made using the initialize method, but it throws an exception. If I remove or comment out the initialize method everything works but the comboboxes won't be populated. – Alinaswe klb May 31 '22 at 09:29
  • The problem is, we can't see the full execution path of the problematic code, and the stack trace is unfortunately unhelpful in this case (it doesn't seem to tell us _where_ the NPE occurred). It will take some effort on your part, but try to reduce your code to a [mre] (← read page; it gives helpful strategies) that demonstrates the problem (it does _nothing else_ but demonstrates the problem). If the database access is a necessary part of the example, first try to _mock_ the access (i.e. don't hit a real database, just return fake data). – Slaw May 31 '22 at 09:58
  • please read the (three times!!) referenced help page and act accordingly – kleopatra May 31 '22 at 14:06
  • Want to double-check something. That stack trace, though I may be wrong, hints at a threading issue. There are only two places in the stack trace that show your own code: `addDataToDB` and `loadAddOrEditWindow`. Can you add `System.out.println(Thread.currentThread())` to both those methods, and tell me what the output is? – Slaw Jun 01 '22 at 12:43

0 Answers0