The code should open a to-do window in which tasks can be specified in a text field. These are stored in a VBox as a checkbox. If the CheckBox are marked as "checked", they will be removed from the list. However, these should be available in a new window using the "Done" button. The Done window can be opened, but the program cannot access the VBox doneBox. NPE is caused by: doneBox.getChildren().add(l); in Controller1
Are there any solutions for this problem?
Error message:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at Controller1.initialize(Controller1.java:93)
at Controller1.handleDoneButton(Controller1.java:79)
... 58 more
Main:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ToDo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Layout1.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("ToDo List");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
}
Controller1:
import java.io.IOException;
import java.util.ArrayList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Controller1 {
ArrayList<CheckBox> list = new ArrayList();
ArrayList<CheckBox> donelist = new ArrayList();
String[] part;
@FXML
VBox taskBox;
@FXML
TextField task;
@FXML
CheckBox c;
@FXML
Button done;
@FXML
VBox doneBox;
@FXML
private void handleAddButton(ActionEvent event) {
String text = task.getText();
c = new CheckBox(text);
list.add(c);
task.clear();
c.setOnAction(new EventHandler<ActionEvent>() {
@Override
@FXML
public void handle(ActionEvent e) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isSelected() == true) {
taskBox.getChildren().remove(list.get(i));
donelist.add(list.get(i));
list.remove(i);
}
}
}
});
taskBox.getChildren().add(c);
}
@FXML
private void handleDoneButton(ActionEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getClassLoader().getResource("Layout2.fxml"));
// new window
Stage stage1 = new Stage();
stage1.setTitle("Done Tasks");
stage1.setScene(new Scene(root, 190, 190));
stage1.setResizable(false);
stage1.show();
initialize();
} catch (IOException e) {
e.printStackTrace();
}
}
// Error in this method:
@FXML
private void initialize() {
for (int i = 0; i < donelist.size(); i++) {
part = donelist.get(i).toString().split("'");
String text = part[1];
Label l = new Label(text);
// Caused by: java.lang.NullPointerException
// at Controller1.initialize(Controller1.java:93):
doneBox.getChildren().add(l);
}
}
}
Layout1:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="303.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller1">
<center>
<ScrollPane prefHeight="328.0" prefWidth="587.0" BorderPane.alignment="CENTER">
<content>
<VBox fx:id="taskBox" prefHeight="309.0" prefWidth="303.0" />
</content>
</ScrollPane>
</center>
<bottom>
<HBox prefHeight="27.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<TextField fx:id="task" prefHeight="25.0" prefWidth="213.0" promptText="Aufgabe hier eingeben" HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="#handleAddButton" prefHeight="25.0" prefWidth="81.0" text="Hinzufügen" />
</children>
</HBox>
</bottom>
<top>
<AnchorPane prefHeight="45.0" prefWidth="600.0" style="-fx-background-color: #2A2C2E;" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" layoutX="118.0" layoutY="4.0" text="ToDo" textFill="#e5e7e9" textOverrun="CLIP">
<font>
<Font name="System Bold" size="26.0" />
</font>
</Label>
<Button fx:id="done" layoutX="252.0" layoutY="11.0" mnemonicParsing="false" onAction="#handleDoneButton" text="Done" />
</children>
</AnchorPane>
</top>
</BorderPane>
Layout2:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="292.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller1">
<top>
<AnchorPane prefHeight="44.0" prefWidth="292.0" style="-fx-background-color: #2A2C2E;" BorderPane.alignment="CENTER">
<children>
<Label layoutX="14.0" layoutY="5.0" text="Done Tasks" textFill="WHITE">
<font>
<Font size="23.0" />
</font>
</Label>
</children>
</AnchorPane>
</top>
<center>
<ScrollPane prefHeight="362.0" prefWidth="292.0" BorderPane.alignment="CENTER">
<content>
<VBox fx:id="doneBox" prefHeight="349.0" prefWidth="283.0" />
</content>
</ScrollPane>
</center>
</BorderPane>