package Data_Project;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public Stage window;
@Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
//Scene1
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent root1 = FXMLLoader.load(getClass().getResource("Admin.fxml"));
Controller a1 = new Controller();
a1.getSubmitButton().setOnAction(e -> {
window.setScene(new Scene(root1,500,500));
});
window.setTitle("Log in");
window.setScene(new Scene(root,500,500));
window.show();
}
public static void main(String[] args) throws IOException {
launch(args);
}
}
嘿那里,我犯这个代码的错误,我无法弄清楚是什么错误。
应用程序启动方法中的异常线程中的异常" main" java.lang.RuntimeException:Application start方法中的异常 com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) 在 com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 147(LauncherImpl.java:157) 在 com.sun.javafx.application.LauncherImpl $$ LAMBDA $ 1 / 989110044.run(未知 来自java.lang.Thread.run(Thread.java:745)引起的: java.lang.NullPointerException at Data_Project.Main.start(Main.java:20)
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Data_Project.Controller">
<children>
<Label text="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="userField" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<PasswordField fx:id="passField" GridPane.columnIndex="2" GridPane.columnSpan="1" GridPane.rowIndex="3" />
<Button fx:id="submitButton" onAction="#submitForm" text="Log in" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="4" />
<Label fx:id="errorLabel" textFill="#c30808" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="6" />
</children>
</GridPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Data_Project.Admin">
<children>
<Label text="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="userField" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<PasswordField fx:id="passField" GridPane.columnIndex="2" GridPane.columnSpan="1" GridPane.rowIndex="3" />
<Button fx:id="submitButton" text="Log in" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="4" />
<Label fx:id="errorLabel" textFill="#c30808" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="6" />
</children>
</GridPane>
XML既是不同的文件,但在这里显示为1个文件,它们具有相同的属性,因为它用于测试场景切换器,但由于某种原因,我无法使其工作!
控制器类:
package Data_Project;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Controller{
Scene window;
@FXML Button submitButton;
@FXML TextField userField;
@FXML PasswordField passField;
@FXML Label errorLabel;
public void submitForm(ActionEvent actionEvent) {
authorizedUser user = new authorizedUser();
if(!user.checkCredentials(userField.getText(), passField.getText()))
{
errorLabel.setText("Invalid credentials");
}
}
public Button getSubmitButton() {
return submitButton;
}
}
管理:
package Data_Project;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Admin{
Scene window;
@FXML Button submitButton;
@FXML TextField userField;
@FXML PasswordField passField;
@FXML Label errorLabel;
public Button getSubmitButton() {
return submitButton;
}
}
答案 0 :(得分:0)
FXMLLoader
解析FXML文件,创建控制器类的实例(如果指定了一个)并将任何@FXML
- 注释字段注入该控制器实例。
如果您创建自己的控制器实例,就像使用
一样Controller a1 = new Controller();
然后FXMLLoader
对该实例一无所知(它将如何?),因此@FXML
- 注释字段未初始化。因此,当你打电话
a1.getSubmitButton()
它返回null
,因此您获得NullPointerException
。
要解决此问题,请替换
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
和
Controller a1 = new Controller();
与
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller a1 = loader.getController();
这为您提供了对FXMLLoader
创建的的控制器实例的引用,因此它已正确初始化了其@FXML
个注释字段。