У меня есть форма входа, к которой я добавил кнопку регистрации (зарегистрироваться). Это работало хорошо, пока я не решил добавить кнопку (действие) в форму регистрации, которая возвращается к форме входа в систему. Если моя форма регистрации без этой функции, она работает хорошо - открывается. Но когда я добавляю функцию возврата, у меня появляется ошибка такого типа:
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.221
javafx.fxml.LoadException:
/C:/Users/Greddy/Exercism/java/SchoolSystem/out/production/SchoolSystem/loginApp/register.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at loginApp.loginController.register(loginController.java:94)
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:1769)
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.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:8411)
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.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:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
`Caused by: java.lang.NullPointerException
at loginApp.loginController.initialize(loginController.java:57)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 66 more
Process finished with exit code 0
Мой register.f xml файл:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="278.0" prefWidth="365.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="loginApp.loginController">
<children>
<Label layoutX="110.0" layoutY="6.0" prefHeight="41.0" prefWidth="227.0" text="Registration Form ">
<font>
<Font size="23.0" />
</font>
</Label>
<Label layoutX="34.0" layoutY="60.0" text="User name" />
<Label layoutX="37.0" layoutY="93.0" text="Password" />
<Label layoutX="34.0" layoutY="131.0" text="Confirm Password" />
<Label layoutX="48.0" layoutY="170.0" text="Email" />
<TextField fx:id="regusername" layoutX="158.0" layoutY="56.0" promptText="Enter username" />
<PasswordField fx:id="regpassword" layoutX="158.0" layoutY="89.0" promptText="Enter your password" />
<PasswordField fx:id="regconfirmpassword" layoutX="158.0" layoutY="127.0" promptText="Confirm your password" />
<TextField fx:id="regEmail" layoutX="158.0" layoutY="166.0" promptText="Enter your email" />
<Button fx:id="backToLogin" layoutX="210.0" layoutY="214.0" mnemonicParsing="false" onAction="#backToLoginF" text="Back to Login" />
</children>
</AnchorPane>
Мой файл loginController (основной контроллер):
package loginApp;
import admin.AdminController;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import students.StudentsController;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class loginController implements Initializable {
LoginModel loginModel = new LoginModel();
@FXML
private Label dbstatus;
@FXML
private TextField username;
@FXML
private PasswordField password;
@FXML
private ComboBox<option> combobox;
@FXML
private javafx.scene.control.Button loginButton;
@FXML
private Label loginStatus;
@FXML
private Button backToLogin;
@FXML
private TextField regusername;
@FXML
private PasswordField regpassword;
@FXML
private PasswordField regconfirmpassword;
@FXML
private TextField regEmail;
@FXML
private Button registerButton;
public void initialize (URL url, ResourceBundle rb){
if(this.loginModel.isDatabaseConnected()){
this.dbstatus.setText("Connected to Database");
} else {
this.dbstatus.setText("Not Connected To Database");
}
this.combobox.setItems(FXCollections.observableArrayList(option.values()));
}
//login to application
public void Login(ActionEvent event){
try {
if (this.loginModel.isLogin(this.username.getText(), this.password.getText(), ((option)this.combobox.getValue()).toString())) {
Stage stage = (Stage) this.loginButton.getScene().getWindow();
stage.close();
switch (((option) this.combobox.getValue()).toString()) {
case "Admin":
adminLogin();
break;
case "Student":
studentLogin();
break;
}
}
else {
this.loginStatus.setText("Wrong credentials");
}
}catch (Exception localException) {
}
}
public void register(ActionEvent event) {
try {
// we are in controller folder, but our view is not here, so we need to go one step up - ../
Parent root = FXMLLoader.load(getClass().getResource("/loginApp/register.fxml"));
Stage stage = new Stage();
stage.setTitle("Register");
stage.setScene(new Scene(root, 400, 280));
stage.setResizable(false);
stage.show();
((Node) (event.getSource())).getScene().getWindow().hide();
} catch (Exception e) {
e.printStackTrace();
}
}
public void backToLoginF(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/loginApp/login.fxml"));
Stage stage = new Stage();
stage.setTitle("Login");
stage.setScene(new Scene(root, 450, 350));
stage.show();
((Node) (event.getSource())).getScene().getWindow().hide();
}
//login with studentlogin
public void studentLogin() throws IOException {
try {
Stage userStage = new Stage ();
FXMLLoader loader = new FXMLLoader();
Pane root = (Pane)loader.load(getClass().getResource("/students/StudentsFXML.fxml").openStream());
StudentsController studentsController = (StudentsController) loader.getController();
Scene scene = new Scene(root);
userStage.setScene(scene);
userStage.setTitle("Student Dashboard");
userStage.setResizable(false);
userStage.show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//login with adminLogin
public void adminLogin(){
try {
Stage adminStage = new Stage();
FXMLLoader adminLoader = new FXMLLoader();
Pane adminroot = (Pane) adminLoader.load(getClass().getResource("/admin/Admin.fxml").openStream());
AdminController adminController = (AdminController)adminLoader.getController();
Scene scene = new Scene(adminroot);
adminStage.setScene(scene);
adminStage.setTitle("Admin DashBoard");
adminStage.setResizable(false);
adminStage.show();
} catch (IOException e){
e.printStackTrace();
}
}
}
Структура проекта
Я проверил кнопки и текстовые поля, они в порядке. Я думаю, что это связано с контроллером и / или с путем к пакету или именами, но я не могу найти, в чем проблема. Я сделал новый проект для теста с той же функциональностью (вернитесь к первой форме, и она заработала). Есть предложения?