我有以下设置:
2 FXML-Doxuments WelcomeScreen.fxml和Timesheet.fxml,它的控制器是WelcomeScreenController.java和TimesheetController.java。
在WelcomeScreenController上我有一个按钮动作处理程序
createTimesheet(ActionEvent event){
// Some working code
final Calendar calendarStart = Calendar.getInstance();
calendarStart.setTime(sdf.parse(tfStartDate.getText()));
final Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(sdf.parse(tfEndDate.getText()));
final FXMLLoader loader = new FXMLLoader(getClass().getResource("Timesheet.fxml"));
final TimesheetControler timesheetControler = (TimesheetControler)loader.getController();
timesheetControler.setStartDate(calendarStart);
timesheetControler.setEndDate(calendarEnd);
timesheetControler.setHeadlineText("Timesheet (" + tfStartDate.getText() + " - " + tfEndDate.getText() + ")");
final Node node=(Node) event.getSource();
final Stage stage=(Stage) node.getScene().getWindow();
final Parent root = (Parent) loader.load();
stage.setScene(new Scene(root));
stage.show();
}
TimesheetController看起来像这样:
package de.liebich.work.report.emb.ui;
public class TimesheetControler implements Initializable {
@FXML
private Label lHeadline;
@FXML
private GridPane gTimesheetCheck;
private Calendar startDate;
private Calendar endDate;
private String user;
private String password;
private String baseURL;
private List<List<Label>> entries;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void setStartDate(Calendar startDate) {
this.startDate = startDate;
}
public void setEndDate(Calendar endDate) {
this.endDate = endDate;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String pw) {
this.password = pw;
}
public void setBaseURL(String baseURL) {
this.baseURL = baseURL;
}
public void setHeadlineText(final String text){
lHeadline.setText(text);
}
}
当我调用createTimesheet时,我收到以下错误:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1449)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1444)
... 44 more
Caused by: java.lang.NullPointerException
at de.liebich.work.report.emb.ui.WelcomeScreenControler.createTimesheet(WelcomeScreenControler.java:133)
... 54 more
我需要更改什么才能使我的代码正常工作?
答案 0 :(得分:0)
呼叫
loader.load();
在行 之前
IMO没有必要解释它的原因。如果您仍然怀疑在评论中提出这个问题。final TimesheetControler timesheetControler = (TimesheetControler) loader.getController();
答案 1 :(得分:0)
使用
final TimesheetControler timesheetControler = (TimesheetControler)loader.load();