我正在研究JavaFX项目,我发现了这样的错误:
Logout
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.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:8413)
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.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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 52 more
Caused by: java.lang.NullPointerException
at gn.kahere.car.manager.view.DrawerContentController.logoutAction(DrawerContentController.java:56)
... 62 more
我的MainApp.java
package gn.kahere.car.manager;
import java.io.IOException;
import gn.kahere.car.manager.view.DrawerContentController;
import gn.kahere.car.manager.view.LoginController;
import gn.kahere.car.manager.view.RootLayoutController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private AnchorPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Car Manager");
// Prevent the window resizing
this.primaryStage.setResizable(false);
showLogin();
}
/**
* Show the login page
*/
public void showLogin() {
try {
// Load login page
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/Login.fxml"));
AnchorPane login = (AnchorPane) loader.load();
// Set the scene containing the login page
Scene scene = new Scene(login);
primaryStage.setScene(scene);
// Give the controller access to the main application
LoginController controller = loader.getController();
controller.setMainApp(this);
// Show the scene
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Initializes the root layout
*/
public void initRootLayout() {
try {
// Load root layout
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (AnchorPane) loader.load();
// Give the controller access to the main application
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
// Get the DrawerContentController
FXMLLoader drawerLoader = new FXMLLoader();
drawerLoader.setLocation(MainApp.class.getResource("view/DrawerContent.fxml"));
drawerLoader.load(); // Load the fxml file
DrawerContentController drawerController = drawerLoader.getController();
drawerController.setMainApp(this);
// Show the scene containing the root layout
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我的RootLayoutController.java
package gn.kahere.car.manager.view;
import java.io.IOException;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.transitions.hamburger.HamburgerBasicCloseTransition;
import gn.kahere.car.manager.MainApp;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
public class RootLayoutController {
// Reference to the main application
@SuppressWarnings("unused")
private MainApp mainApp;
@FXML
private JFXHamburger hamburger;
@FXML
private JFXDrawer drawer;
/**
* Controller
*/
public RootLayoutController() {
}
/**
* Initializes the controller class.
*/
@FXML
private void initialize() {
try {
VBox box = FXMLLoader.load(getClass().getResource("DrawerContent.fxml"));
drawer.setSidePane(box);
HamburgerBasicCloseTransition burgerTask = new HamburgerBasicCloseTransition(hamburger);
burgerTask.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED, (e) -> {
burgerTask.setRate(burgerTask.getRate() * -1);
burgerTask.play();
if(drawer.isShown())
drawer.close();
else
drawer.open();
});
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Is called by the main application to give a reference back to itself
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
RootLayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="700.0" prefWidth="1000.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gn.kahere.car.manager.view.RootLayoutController">
<children>
<JFXHamburger fx:id="hamburger" layoutX="946.0" layoutY="24.0" stylesheets="@style.css" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</JFXHamburger>
<JFXDrawer fx:id="drawer" defaultDrawerSize="200.0" prefHeight="700.0" prefWidth="200.0" stylesheets="@../../../../../../bin/gn/kahere/car/manager/view/style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0">
<effect>
<DropShadow />
</effect>
</JFXDrawer>
</children>
</AnchorPane>
DrawerContentController.java
package gn.kahere.car.manager.view;
import gn.kahere.car.manager.MainApp;
import javafx.fxml.FXML;
public class DrawerContentController {
// Reference to the main application
private MainApp mainApp;
/**
* Controller
*/
public DrawerContentController() {
}
@FXML
private void initialize() {
System.out.println("Drawer");
}
@FXML
private void homeAction() {
System.out.println("Home");
}
@FXML
private void addCarAction() {
System.out.println("Add Car");
}
@FXML
private void listCarsAction() {
System.out.println("List Cars");
}
@FXML
private void searchAction() {
System.out.println("Search");
}
@FXML
private void agentsAction() {
System.out.println("Agents");
}
@FXML
private void profileAction() {
System.out.println("Profile");
}
@FXML
private void logoutAction() {
System.out.println("Logout");
mainApp.showLogin();
}
/**
* Is called by the main application to give a reference back to itself
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
DrawerContent.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import java.lang.String?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.text.TextFlow?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="200.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gn.kahere.car.manager.view.DrawerContentController">
<children>
<Pane prefHeight="150.0" prefWidth="200.0" styleClass="side-menu-header" stylesheets="@style.css">
<children>
<Text fill="WHITE" layoutX="14.0" layoutY="131.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MAMADOU ALIOU DIALLO">
<font>
<Font size="12.0" />
</font>
</Text>
<ImageView fitHeight="62.0" fitWidth="66.0" layoutX="14.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../../../img/user/avatar.png" />
</image>
</ImageView>
</children>
</Pane>
<TextFlow prefHeight="18.0" prefWidth="200.0" style="-fx-background-color: #f0f8ff;" styleClass="navigation" stylesheets="@style.css">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="PRINCIPAL NAVIGATION" wrappingWidth="176.49267578125" />
</children>
</TextFlow>
<JFXButton buttonType="RAISED" onAction="#homeAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="HOME" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<Separator prefWidth="200.0" />
<JFXButton buttonType="RAISED" onAction="#addCarAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="ADD CAR" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<Separator prefWidth="200.0" />
<JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#listCarsAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="LIST CARS" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<Separator prefWidth="200.0" />
<JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#searchAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="SEARCH" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<TextFlow layoutX="10.0" layoutY="160.0" prefHeight="18.0" prefWidth="200.0" style="-fx-background-color: #f0f8ff;" styleClass="navigation" stylesheets="@style.css">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="MANAGER" wrappingWidth="176.49267578125" />
</children>
</TextFlow>
<JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#agentsAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="AGENTS" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<Separator prefWidth="200.0" />
<JFXButton buttonType="RAISED" layoutX="10.0" layoutY="408.0" onAction="#profileAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="PROFILE" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
<Separator prefWidth="200.0" />
<JFXButton buttonType="RAISED" layoutX="10.0" layoutY="441.0" onAction="#logoutAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="LOGOUT" textFill="WHITE">
<font>
<Font size="16.0" />
</font>
<styleClass>
<String fx:value="menu-btn" />
<String fx:value="btn" />
</styleClass>
</JFXButton>
</children>
</VBox>
当我点击DefaultContentController中的注销按钮时出现错误,&#34; Logout&#34;显示消息,但我不理解该错误。请帮忙。 THKS。
答案 0 :(得分:2)
您在DrawerContent.fxml
的{{1}}方法中加载并显示 initialize()
:
RootLayoutController
当然,这会创建一个新的public class RootLayoutController {
// Reference to the main application
@SuppressWarnings("unused")
private MainApp mainApp;
// ...
/**
* Initializes the controller class.
*/
@FXML
private void initialize() {
try {
VBox box = FXMLLoader.load(getClass().getResource("DrawerContent.fxml"));
drawer.setSidePane(box);
// ...
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Is called by the main application to give a reference back to itself
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
链接到从DrawerContentController
创建的视图。在任何时候,您都不会在该控制器上调用DrawerContent.fxml
,因此在setMainApp(...)
的实例中,DrawerContentController
为空。因此,当用户按下mainApp
按钮并调用处理程序方法时,您将获得空指针异常。
您需要初始化LOGOUT
中的mainApp
。你可以用:
DrawerContentController
显然,您可以从加载FXML文件的import java.io.IOException;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.transitions.hamburger.HamburgerBasicCloseTransition;
import gn.kahere.car.manager.MainApp;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
public class RootLayoutController {
// Reference to the main application
// @SuppressWarnings("unused") // hmm if it's unused, I probably don't need it...
// private MainApp mainApp;
private DrawerContentController drawerContentController ;
@FXML
private JFXHamburger hamburger;
@FXML
private JFXDrawer drawer;
/**
* Controller
*/
public RootLayoutController() {
}
/**
* Initializes the controller class.
*/
@FXML
private void initialize() {
try {
FXMLLoader drawerContentLoader = new FXMLLoader(getClass().getResource("DrawerContent.fxml"));
VBox box = drawerContentLoader.load();
drawer.setSidePane(box);
drawerContentController = drawerContentLoader.getController();
HamburgerBasicCloseTransition burgerTask = new HamburgerBasicCloseTransition(hamburger);
burgerTask.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED, (e) -> {
burgerTask.setRate(burgerTask.getRate() * -1);
burgerTask.play();
if(drawer.isShown())
drawer.close();
else
drawer.open();
});
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Is called by the main application to give a reference back to itself
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
// this.mainApp = mainApp;
// pass the main app to the drawerContentController:
drawerContentController.setMainApp(mainApp);
}
}
类中删除所有冗余代码,并且从不显示其内容(为什么还要这样做?):
MainApp