所以我正在使用javafx和场景构建器开发一个应用程序。我已经创建了一个LogIn UI和一个主UI.And,因为我需要根据条件加载各种UI我创建了一个LoadUI类,它将加载所有给定fxml文件的我的UI作为字符串传递。但问题是程序在运行程序时显示一堆FXML加载程序错误。只有当我将onAction添加到我的按钮时才会显示这些错误,如果我将它们保持为空,程序运行正常。所以,任何人都知道我做错了什么。
我的LoadUI类
public class LoadUI {
public static Scene loadUI(Scene scene, String UIName,int width, int height){
FXMLLoader loader=new FXMLLoader();
loader.setLocation(LoadUI.class.getResource(UIName));
try {
scene=new Scene(loader.load(),width,height);
} catch (IOException ex) {
Logger.getLogger(LoadUI.class.getName()).log(Level.SEVERE, null, ex);
}
return scene;
}
}
我的LogInController
public class LoginController implements Initializable{
/**
* Initializes the controller class.
*/
@FXML private TextField username;
@FXML private PasswordField password;
@FXML private Button btnLogIn;
@FXML private Button btnCancel;
public String user = username.getText();
public String userPassword =password.getText();
public Stage stage;
public void handleLogIn(ActionEvent ae){
Connection con;
ResultSet rs;
try {
con =DBConnector.connect();
PreparedStatement ps=con.prepareStatement("SELECT * FROM employee Where Employee_Name=? and Password = ?");
ps.setString(1,user);
ps.setString(2,userPassword);
rs=ps.executeQuery();
if (rs.getString("Employee_Name")==user){
if(rs.getString("Password")==userPassword){
Scene sc =null;
stage.setTitle("Student Information System");
stage.setScene(LoadUI.loadUI(sc,"StudentView.fxml" , 1000,600 ));
stage.show();
}
}
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void handleCancel(ActionEvent ae){
System.out.close();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
btnLogIn.setOnAction(this::handleLogIn);
btnCancel.setOnAction(this::handleCancel);
}
}
我的LogIn.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="374.0" prefWidth="849.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="myproject.LoginController">
<children>
<AnchorPane layoutX="519.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="402.0" prefWidth="330.0" style="-fx-background-color: #3678d3;">
<children>
<Label layoutX="15.0" layoutY="139.0" prefHeight="28.0" prefWidth="273.0" text="Username" textFill="WHITE">
<font>
<Font name="Century Gothic Bold" size="18.0" />
</font>
</Label>
<TextField fx:id="Username" layoutX="11.0" layoutY="167.0" prefHeight="34.0" prefWidth="286.0" promptText="Enter Username" style="-fx-background-color: #3678d3;">
<font>
<Font size="15.0" />
</font>
</TextField>
<Label layoutX="15.0" layoutY="226.0" prefHeight="28.0" prefWidth="273.0" text="Password" textFill="WHITE">
<font>
<Font name="Century Gothic Bold" size="18.0" />
</font>
</Label>
<PasswordField fx:id="Password" layoutX="14.0" layoutY="254.0" prefHeight="34.0" prefWidth="286.0" promptText="Enter Password" style="-fx-background-color: #3678d3;">
<font>
<Font size="15.0" />
</font>
</PasswordField>
<Separator layoutX="11.0" layoutY="200.0" prefHeight="3.0" prefWidth="286.0" />
<Separator layoutX="14.0" layoutY="288.0" prefHeight="3.0" prefWidth="286.0" />
<Label alignment="CENTER" layoutX="98.0" layoutY="25.0" prefHeight="66.0" prefWidth="146.0" text="Log In" textFill="WHITE">
<font>
<Font name="Century Gothic" size="36.0" />
</font>
</Label>
<Button fx:id="btnLogIn" layoutX="38.0" layoutY="319.0" mnemonicParsing="false" onAction="#handleLogIn" prefHeight="34.0" prefWidth="89.0" text="Log In">
<font>
<Font size="15.0" />
</font></Button>
<Button fx:id="btnCancel" layoutX="199.0" layoutY="319.0" mnemonicParsing="false" prefHeight="34.0" prefWidth="89.0" text="Cancel">
<font>
<Font size="15.0" />
</font></Button>
</children>
</AnchorPane>
<AnchorPane prefHeight="402.0" prefWidth="302.0">
<children>
<ImageView fitHeight="402.0" fitWidth="521.0" pickOnBounds="true">
<image>
<Image url="@../prime.jpg" />
</image>
</ImageView>
</children>
</AnchorPane>
</children>
</AnchorPane>
我还有一个主要类,它将初始加载logIn UI,从LoginController开始,剩余的UI将根据所需的条件加载,我现在编写的代码只加载了1个特定的UI。
我的主要课程
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Scene scene= null;
primaryStage.setResizable(false);
primaryStage.setTitle("Hello World");
primaryStage.setScene(LoadUI.loadUI(scene, "Login.fxml",839,374));
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
错误是:
Jul 17, 2017 6:08:54 PM myproject.LoadUI loadUI
SEVERE: null
javafx.fxml.LoadException:
file:/C:/Users/User/Documents/NetBeansProjects/MyProject/dist/run968915114/MyProject.jar!/myproject/Login.fxml:13
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at myproject.LoadUI.loadUI(LoadUI.java:23)
at myproject.Main.start(Main.java:43)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1456904334.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1753953479.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at myproject.LoginController.<init>(LoginController.java:39)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
... 18 more