StudenDetails类
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
public class StudentDetails {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private Button okButton;
private String firstName;
@FXML
private void handleOkButtonOnAction(ActionEvent event) throws IOException {
if (firstNameField.getText().trim().isEmpty() || lastNameField.getText().trim().isEmpty()){
firstNameField.setStyle("-fx-border-color: red");
lastNameField.setStyle("-fx-border-color: red");
}else {
Parent qualification = FXMLLoader.load(getClass().getResource("qualification.fxml"));
Scene qualificationScene = new Scene(qualification, 1050, 600);
Stage qualificationStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
qualificationStage.setResizable(false);
qualificationStage.setScene(qualificationScene);
qualificationStage.show();
firstNameField.setStyle("-fx-border-color: darkgray");
lastNameField.setStyle("-fx-border-color: darkgray");
firstName = firstNameField.getText().trim();
System.out.println(firstNameField.getText().trim() + " h");
}
}
public String getFirstName() {
System.out.println(firstName + "get");
return firstName;
}
}
StudentDetails.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<GridPane fx:controller="sample.StudentDetails" id="studentGridPane"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10" stylesheets="@styleCareer.css">
<Label id="studentHead" text="Please enter your details as appropriate" alignment="CENTER" GridPane.rowIndex="0" GridPane.columnSpan="2"/>
<Label text="First Name" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="firstNameField" promptText="Enter your first name" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Last Name" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<TextField fx:id="lastNameField" promptText="Enter your last name" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button text="OK" GridPane.columnIndex="1" onMouseEntered="#handleOkButtonOnMouseEntered" onMouseExited="#handleOkButtonOnMouseExited" onAction="#handleOkButtonOnAction"
fx:id="okButton" GridPane.rowIndex="6" id="studentButton" alignment="BOTTOM_RIGHT" GridPane.halignment="RIGHT"/>
</GridPane>
资格等级
public class Qualification {
@FXML
private Label setName;
public void initialize() {
StudentDetails studentDetails = new StudentDetails();
setName.setText(studentDetails.getFirstName());
System.out.println(studentDetails.getFirstName() + " Q");
}
}
Qualification.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.CheckBox?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.GridPane?>
<GridPane id="qualification" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Qualification" stylesheets="@styleCareer.css" alignment="CENTER">
<Label fx:id="setName" GridPane.halignment="LEFT" GridPane.rowIndex="0" GridPane.columnIndex="0" text=""/>
问题:
错误不断出现 “由:java.lang.NullPointerException 在sample.StudentDetails。(StudentDetails.java:23) 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处 在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在java.lang.Class.newInstance(Class.java:442) 在sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) 在javafx.fxml.FXMLLoader $ ValueElement.processAttribute(FXMLLoader.java:927) 在javafx.fxml.FXMLLoader $ InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) 在javafx.fxml.FXMLLoader $ Element.processStartElement(FXMLLoader.java:220) 在javafx.fxml.FXMLLoader $ ValueElement.processStartElement(FXMLLoader.java:744) 在javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ...还有66个”
当尝试从StudentDetailClass中的textField检索值以及将值设置为限定类中的setName标签的文本时,我得到NPE(空指针异常)。