I am new to Javafx, and in doing a little project I encountered a problem. This program is supposed to be a little scheduling program, in which the user is supposed to be able to make events in scene2 and scene4 and view them in scene3.fxml. After the events are made and the user navigates to scene3.fxml, it is supposed to change the label to the list of events (i tried a listView, but that had other errors). For some reason, whenever I try to access "scene3.fxml" I get the stack of errors below, and even though I see the scene it doesn't work as intended. I have ensured all fxids are correct, and all the other fxml files are working properly. package application; SceneController.java:
package application;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.util.ArrayList;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent root;
//Scene2
@FXML
private DatePicker myDatePicker;
@FXML
private Label dateLabel;
@FXML
private Label errorDate;
//Scene3
@FXML
private Label listLabel;
@FXML
private Label ScheduleLabel;
@FXML
private Button returnFrom3;
//Scene4
@FXML
private TextArea description;
@FXML
private TextField name;
@FXML
private Button eventSubmitter;
@FXML
private Label errorLabel;
public static ArrayList<String> events = new ArrayList<String>();
public static ArrayList<String> descriptions = new ArrayList<String>();
public static ArrayList<LocalDate> dates = new ArrayList<LocalDate>();
private LocalDate myDate;
ImageView myImageView;
String currentEvent;
Image myImage = new Image(getClass().getResourceAsStream("SchedulerLogo.PNG"));
/*
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
/*
listLabel.getItems().addAll(events);
listLabel.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
// TODO Auto-generated method stub
currentEvent = listLabel.getSelectionModel().getSelectedItem();
}
});
}
*/
public String getDate() {
try {
myDate = myDatePicker.getValue();
System.out.println(myDate.toString());
dates.add(myDate);
return myDate.toString();
}catch(NullPointerException e) {
System.out.println("NullPointerException e");
return "";
}catch(Exception e) {
System.out.println("exception e");
LocalDate myDate = myDatePicker.getValue();
dates.add(myDate);
System.out.println(myDate.toString());
return myDate.toString();
}
}
public void switchToScene1(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("fxml.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
//myImageView.setImage(myImage);
}
public void switchToScene2(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void loadEvents() {
for(int i =0; i<events.size(); i++) {
currentEvent = currentEvent + events.get(i);
}
listLabel.setText(currentEvent);
}
public void switchToScene3(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("scene3.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
loadEvents();
}
public void submitDate(ActionEvent event) throws IOException {
if (myDate != null) {
System.out.println(myDate + "numba 2 ");
//dates.add();
Parent root = FXMLLoader.load(getClass().getResource("scene4.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
} else {
errorDate.setVisible(true);
}
printArrays();
}
public void submitEvent(ActionEvent event) throws IOException{
String eventName = name.getText();
String eventDescription = description.getText();
if(eventName.length()>0) {
events.add(eventName);
descriptions.add(eventDescription);
errorLabel.setVisible(true);
errorLabel.setText("Your event has been submitted. You may now navigate back to the main menu.");
}else {
errorLabel.setVisible(true);
}
printArrays();
}
public void printArrays() {
System.out.println(events);
System.out.println(descriptions);
System.out.println(dates);
}
}
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.image.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.text.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.DatePicker;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
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.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.image.*;
import java.util.ArrayList;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
try {
LocalDate e = LocalDate.now();
SceneController.events.add(" ");
SceneController.descriptions.add(" ");
SceneController.dates.add(e);
Parent root = FXMLLoader.load(getClass().getResource("fxml.fxml"));
Scene scene = new Scene(root);
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
Image icon = new Image("SchedulerLogo.PNG");
stage.getIcons().add(icon);
stage.setTitle("Scheduler");
stage.setWidth(1000);
stage.setHeight(700);
stage.setFullScreen(false);
stage.setFullScreenExitHint("press q to escape?");
stage.setFullScreenExitKeyCombination(KeyCombination.valueOf("q"));
stage.setScene(scene);
stage.show();
}catch(Exception e) {
e.printStackTrace();
}
}
}
scene3.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1255.0" prefWidth="1299.0" style="-fx-background-color: pink;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController">
<children>
<Label fx:id="ScheduleLabel" alignment="CENTER" layoutX="-4.0" layoutY="73.0" prefHeight="17.0" prefWidth="1309.0" style="-fx-background-color: pink;" text="Upcoming Events:">
<font>
<Font size="75.0" />
</font>
</Label>
<Button fx:id="returnFrom3" layoutX="625.0" layoutY="1136.0" mnemonicParsing="false" onAction="#switchToScene1" prefWidth="94.0" text="Main Menu" />
<Label fx:id="listLabel" alignment="TOP_LEFT" layoutY="301.0" prefHeight="821.0" prefWidth="1299.0" text="Label" />
</children>
</AnchorPane>
error stack-trace:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1858)
at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1726)
at javafx.base@20.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base@20.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base@20.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base@20.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@20.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base@20.0.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@20.0.1/javafx.scene.Node.fireEvent(Node.java:8944)
at javafx.controls@20.0.1/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls@20.0.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at javafx.controls@20.0.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base@20.0.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base@20.0.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base@20.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232)
at javafx.base@20.0.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189)
at javafx.base@20.0.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base@20.0.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base@20.0.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base@20.0.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base@20.0.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics@20.0.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3980)
at javafx.graphics@20.0.1/javafx.scene.Scene.processMouseEvent(Scene.java:1890)
at javafx.graphics@20.0.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2704)
at javafx.graphics@20.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics@20.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@20.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics@20.0.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics@20.0.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics@20.0.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics@20.0.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics@20.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@20.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
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:72)
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@20.0.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:270)
at javafx.fxml@20.0.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1853)
... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.listLabel" is null
at hellofx/application.SceneController.loadEvents(SceneController.java:157)
at hellofx/application.SceneController.switchToScene3(SceneController.java:166)
... 57 more
i have tried recreating the fxml files, I think that all the libraries and build paths are in order, I have no idea what is happening. Please help.