So i'm developing an application using javafx and scene builder.I've created a LogIn UI and a main UI.And, since I need to load various UI depending upon the condtion I created a LoadUI class which would load all my UI given the fxml file is passed as a string.But the problem is the program show a bunch of FXML loader errors when I run my program. These errors are only shown once I've added an onAction to my buttons if I keep them empty the programs run fine. So, anyone has any idea what I'm doing wrong.
my LoadUI class
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;
}
}
my 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);
}
}
my 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>
And I also Have a main class which will initailly load the logIn UI and from LoginController the remaining UI will be loaded based on the required conditions right now I've written codes to load only 1 particular UI.
my main class
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);
}
the errors are:
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