In my Gluon Mobile project, I want to allow the user to switch from light to dark mode. However, as I discovered through my Stack Question: Allowing user to switch between light and dark mode Gluon, I need to access the Scene through the getScene() method. Everything was working properly until I tried to save the user preferences in a data file I have for each user session; it no longer works. Not only am I getting an error (see below), but it is also not displaying the correct user preferences (it only displays the default until I use the in-app button to change it). The goal is for the user to be able to press the in-app button to change the mode and have that mode be automatically shown the next time the user opens the app. I understand that this is a specific question to my project that is probably not appropriate for Stack Overflow, but I don't know how else I could figure this out. So, I would really appreciate if someone could please help me out. I am almost finished with my app, and I am in a bit of a time crunch. Thank you so much for your time, and I look forward to hearing back from you.
P.S. let me know if you would like the link to my GitHub Repo for this project.
(Relevant part of) code for boolean light (mode indicator) that is being saved in Session class:
public class Session implements Serializable {
/**
* Data field: userid user name
* Data field: password user password
* Data field: date the date and time the session started
* Data field: feeling1 mood (1..100) of user at the beginning of session
* Data field: feeling2 mood (1..100) of user at the end of session
* Data field: conversation sentences expressed by the user during a session
*/
private String userid = "";
private String password = "";
private boolean light = true;
private Date date = new Date();
private Date time = new Date();
private double feeling1 = 0;
private double feeling2 = 0;
private ArrayList<String> conversation = new ArrayList<>();
private ArrayList<String> times = new ArrayList<>();
private static final long serialVersionUID = 2L;
/**
* The only constructor: It receives a user's id and password
* and starts a session associated with the user. It records
* the date and time the session started.
*
* @param id user id
* @param pass user password
*/
public Session(String id, String pass) {
userid = id;
password = pass;
date = new Date();
}
/**
* Reset mode of user for this session: The method receives a mode (light or dark)
* and replaces current mode with the received mode.
*
* @param dark the new mode
*/
public void setMode(boolean dark) {
light = dark;
}
/**
* Get mode (light or dark) of the user for this session
*
* @return mode of user for this session
*/
public boolean getMode() {
return light;
}
Code in PrimaryView class that is giving me the error:
// Use a GridPane to create a login interface insights
VBox grid = new VBox();
//grid.setStyle("-fx-background-color: black;");
grid.setAlignment(Pos.CENTER);
Insets snI = new Insets(10, 10, 10, 10);
grid.setPadding(snI);
Button button = new Button();
grid.getChildren().add(button);
if (session.getMode() == true) {
light = true;
LIGHT.assignTo(grid.getScene()); // ERROR IS POINTING TO THIS LINE
button.setGraphic(new Icon(MaterialDesignIcon.BRIGHTNESS_2));
} else if (session.getMode() == false) {
light = false;
DARK.assignTo(grid.getScene());
button.setGraphic(new Icon(MaterialDesignIcon.WB_SUNNY));
}
The error itself:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Scene cannot be null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at com.gluonhq.impl.charm.glisten.util.StylesheetTools.replaceStylesheet(StylesheetTools.java:64)
at com.gluonhq.charm.glisten.visual.Theme.assignTo(Theme.java:34)
at com.mysecondapplication.views.PrimaryView.homeScreen(PrimaryView.java:276)
at com.mysecondapplication.views.PrimaryView.lambda$new$0(PrimaryView.java:183)
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.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:8889)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
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.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:3856)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
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(Native Method)
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.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Edit: I did some testing which printed that the Scene is neither light nor dark?? I am quite confused.