[每当我尝试访问程序中的编辑菜单时,都会在我的selectiondialogcontroller类initialize方法的第一行出现nullpointer异常,这是如果我尝试使用可观察列表设置列表项而不用初始化它的情况。声明中的新关键字。我不确定为什么会发生这种情况,但是我通过使用new并使用可观察的列表构造了该项目,尽管该项目未显示在gui的listview中。
请帮助!是什么导致这些问题?在我的主窗口控制器中,我什至不必考虑这个问题,它使用@fxml初始化并设置项目没有问题!
我的堆栈跟踪:
"C:\Program Files\Java\jdk-13.0.1\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -javaagent:C:\Users\omara\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\201.7223.91\lib\idea_rt.jar=62311:C:\Users\omara\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\201.7223.91\bin -Dfile.encoding=UTF-8 -p C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.base.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.graphics.jar;D:\MyToDoList\out\production\MyToDoList;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx-swt.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.controls.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.fxml.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.media.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.swing.jar;C:\Users\omara\Documents\lib\javafx-sdk-11.0.2\lib\javafx.web.jar -m MyToDoList/com.company.todolist.Main couldnt load dialog javafx.fxml.LoadException: /D:/MyToDoList/out/production/MyToDoList/com/company/todolist/SelectionDialog.fxml at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466) at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435) at MyToDoList/com.company.todolist.Controller.showEditDialog(Controller.java:99) 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 com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) 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:567) at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784) at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 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.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.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:465) at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1380) at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.lambda$createChildren$12(ContextMenuContent.java:1333) at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 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:3851) at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579) at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849) at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390) at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433) 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:830) 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:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) 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:567) at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591) ... 57 more Caused by: java.lang.NullPointerException at MyToDoList/com.company.todolist.SelectionDialogController.initialize(SelectionDialogController.java:16) ... 68 more Process finished with exit code 0
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <DialogPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.company.todolist.SelectionDialogController" prefHeight="400.0" prefWidth="600.0"> <content> <BorderPane> <left> <ListView fx:id="selectionlistviewq"/> </left> </BorderPane> </content> </DialogPane>
package com.company.todolist; import com.company.todolist.datamodel.TodoData; import com.company.todolist.datamodel.todoitem; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.control.*; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.util.Callback; import java.io.IOException; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Optional; public class Controller { @FXML private BorderPane mainborderpane; @FXML private Label datelabel; @FXML private ListView<todoitem> todolistview; @FXML private TextArea vboxtext; public void initialize() { todolistview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<todoitem>() { @Override public void changed(ObservableValue<? extends todoitem> observableValue, todoitem todoitem, todoitem t1) { if (t1 != null) { todolistview.getSelectionModel().select(t1); vboxtext.setText(t1.getDetails()); DateTimeFormatter df = DateTimeFormatter.ofPattern("MMM - dd - yyyy"); datelabel.setText(df.format(t1.getDeadline())); } } }); todolistview.setItems(TodoData.getInstance().getTodoitemList()); todolistview.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); todolistview.getSelectionModel().selectFirst(); todolistview.setCellFactory(new Callback<ListView<todoitem>, ListCell<todoitem>>() { @Override public ListCell<todoitem> call(ListView<todoitem> todoitemListView) { ListCell<todoitem> cell = new ListCell<>() { @Override protected void updateItem(todoitem todoitem, boolean b) { super.updateItem(todoitem, b); if (b) { setText(null); } else { setText(todoitem.getName()); if (todoitem.getDeadline().isBefore(LocalDate.now())) { setTextFill(Color.BROWN); } if (todoitem.getDeadline().isEqual(LocalDate.now())) { setTextFill(Color.RED); } Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, 1); Date tomorrow = calendar.getTime(); LocalDate date = tomorrow.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); if (todoitem.getDeadline().equals(date)) { setTextFill(Color.GREENYELLOW); } } } }; return cell; } }); } @FXML public void clicklistview() { todoitem item = todolistview.getSelectionModel().getSelectedItem(); vboxtext.setText(item.getDetails()); datelabel.setText(item.getDeadline().toString()); } @FXML public void showEditDialog() { Dialog<ButtonType> dialog = new Dialog<>(); dialog.initOwner(mainborderpane.getScene().getWindow()); FXMLLoader myloader = new FXMLLoader(); myloader.setLocation(getClass().getResource("SelectionDialog.fxml")); try { dialog.getDialogPane().setContent(myloader.load()); } catch (IOException e) { System.out.println("couldnt load dialog"); e.printStackTrace(); return; } dialog.setTitle("Edit an item"); dialog.getDialogPane().getButtonTypes().add(ButtonType.OK); dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); Optional<ButtonType> hmm = dialog.showAndWait(); if (hmm.isPresent() && hmm.get().equals(ButtonType.OK)) { todoitem myitem = saveselecteditem(); dialog.close(); Dialog<ButtonType> dialog2 = new Dialog<>(); dialog.initOwner(mainborderpane.getScene().getWindow()); FXMLLoader myloader2 = new FXMLLoader(); myloader2.setLocation(getClass().getResource("editDialog.fxml")); try { dialog2.getDialogPane().setContent(myloader.load()); } catch (IOException e) { System.out.println("this"); System.out.println("couldnt load dialog"); e.printStackTrace(); return; } dialog2.setTitle("Do your edit"); } } public todoitem saveselecteditem() { todoitem newitem = SelectionDialogController.getlist().getSelectionModel().getSelectedItem(); return newitem; } @FXML public void shownewdialog() { Dialog<ButtonType> dialog = new Dialog<>(); dialog.initOwner(mainborderpane.getScene().getWindow()); dialog.setTitle("Add new item.."); dialog.setHeaderText("This is the header text !"); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getResource("TodoitemDialog.fxml")); try { dialog.getDialogPane().setContent(fxmlLoader.load()); } catch (IOException e) { System.out.println("new"); System.out.println("couldnt load dialog"); e.printStackTrace(); return; } dialog.getDialogPane().getButtonTypes().add(ButtonType.OK); dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL); Optional<ButtonType> result = dialog.showAndWait(); if (result.isPresent() && result.get() == ButtonType.OK) { DialogController controller = fxmlLoader.getController(); todoitem itemtoselect = controller.process(); todolistview.getSelectionModel().select(itemtoselect); } } }
package com.company.todolist; import com.company.todolist.datamodel.TodoData; import com.company.todolist.datamodel.todoitem; import javafx.fxml.FXML; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.util.Callback; public class SelectionDialogController { @FXML private static ListView<todoitem> selectionlistviewq = new ListView<>(TodoData.getInstance().getTodoitemList()); public void initialize(){ System.out.println("Called"); System.out.println(selectionlistviewq.getItems().get(1).getName()); selectionlistviewq.getSelectionModel().selectFirst(); selectionlistviewq.setCellFactory(new Callback<ListView<todoitem>, ListCell<todoitem>>() { @Override public ListCell<todoitem> call(ListView<todoitem> todoitemListView) { ListCell<todoitem> cell = new ListCell<>(){ @Override protected void updateItem(todoitem todoitem, boolean b) { super.updateItem(todoitem, b); if(b){ setText(null); }else{ setText(todoitem.getName()); } } }; return cell; } }); } public static ListView<todoitem> getlist(){ return selectionlistviewq; } }
[每当我尝试访问程序中的编辑菜单时,都会在我的selectiondialogcontroller类初始化方法的第一行出现nullpointer异常,这是如果我尝试设置...的项目的话]]
private static ListView<todoitem> selectionlistviewq = new ListView<>(TodoData.getInstance().getTodoitemList());