我已经被困在编辑标签的内容几天了。我正在创建一个应用程序,我在开始时加载login.fxml,然后将fxml更改为例如admin_login.fxml,但是当我再次重新加载login.fxml并尝试使用.setText设置Label文本时它会给我一个空指针异常。 如果有人可以帮我解决这个问题,我真的很感激。
Main.java
public class Main extends Application {
Parent root;
FXMLLoader loader;
String login[][] = { {"admin" ,"admin1234" },
{"student","student1234"},
{"teacher","teacher1234"} };
@Override
public void start(Stage primaryStage) throws Exception{
loader = new FXMLLoader();
loader.setLocation(getClass().getResource("login.fxml"));
root = loader.load();
primaryStage.setTitle("Practice Test Management System");
primaryStage.setScene(new Scene(root, 1000, 700));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
enter code here@FXML Button login_login_bn ;
@FXML TextField login_user_id;
@FXML TextField login_pass;
@FXML Label login_login_error;
@FXML Label login_connection_msg;
@FXML Button admin_set_bn;
@FXML Button admin_about_bn;
@FXML Button admin_users_bn;
@FXML Button teacher_students_bn;
@FXML Button teacher_subjects_bn;
@FXML Button teacher_tests_bn;
@FXML Button teacher_results_bn;
@FXML Button teacher_ques_bn;
@FXML Button teacher_about_bn;
@FXML Button student_about_bn;
@FXML Button student_tests_bn;
@FXML Button student_results_bn;
@FXML Button logout_bn;
@FXML Button login_condatabase_bn;
@FXML TextField super_user_id;
@FXML TextField super_pass;
@FXML Label super_msg_lb;
@FXML Button super_login_bn;
@FXML TextField login_db_name;
@FXML TextField login_db_user_id;
@FXML TextField login_db_pass;
@FXML Label login_db_connection_msg_lb;
@FXML Button login_db_connect_bn;
//Variables
Boolean adminlogin = false;
Boolean studentlogin = false;
Boolean teacherlogin = false;
//Classes
Admin admin = new Admin();
Main main = new Main();
CurrentUser currentuser = new CurrentUser();
DatabaseDriver databasedriver = new DatabaseDriver();
//Scenes
Scene login_scene;
public void dbConnected() throws IOException {
currentuser.reset();
Stage stage1;
Parent root;
stage1 = (Stage) login_db_connect_bn.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene2 = new Scene(root, 1000, 700);
login_connection_msg.setText("database Connected"); //This isn't Working :(
stage1.setScene(scene2);
stage1.show();
}
public void superWindow() throws IOException{
Stage stage1;
stage1 = (Stage) login_condatabase_bn.getScene().getWindow();
Parent root;
root = FXMLLoader.load(getClass().getResource("super_login.fxml"));
Scene scene2 = new Scene(root,400,400);
stage1.setScene(scene2);
stage1.show();
}
public void superLogin() throws IOException {
if (admin.adminLogin(super_user_id.getText(),super_pass.getText())) {
Stage stage1;
Parent root;
stage1 = (Stage) super_login_bn.getScene().getWindow();
login_scene = (Scene) super_login_bn.getScene();
root = FXMLLoader.load(getClass().getResource("login_database_connect.fxml"));
Scene scene2 = new Scene(root, 600, 400);
stage1.setScene(scene2);
stage1.show();
} else {
super_msg_lb.setText("Invalid User Id & Password");
}
}
public void databaseConnect() throws IOException {
Boolean connection ;
connection = databasedriver.databaseConnection(login_db_name.getText(),login_db_user_id.getText(),login_db_pass.getText());
if(connection){
this.dbConnected();
}else{
System.out.println("connection false");
login_db_connection_msg_lb.setText("Connection not successful....Try Again !");
}
}
}
在Main中的start函数中加载login.fxml ...然后在控制器中执行superWindow()然后它在控制器中执行superLogin()然后它运行databaseConnect()然后在我加载时运行dbConnected但在dbConnected中运行login.fxml并编辑标签login_connection_msg
login_connection_msg.setText("database Connected");
然后它给了我一个空指针Exception:(