【发布时间】:2020-10-10 18:57:02
【问题描述】:
我已成功从数据库中检索数据。其中一个变量是我想在 DashboardController 中使用的 uniqueiddb。我需要它,因为我必须查询数据库中各个用户的数据。但它是在 LoginController 中检索到的。我必须将其移至 DashboardController。我尝试使用 setter 来设置 LoginController 中的值。当我在 DashboardController 中使用 getter 时,我得到一个 NullPointException。这意味着该值未设置。我不明白为什么。有人请指出我哪里出错了。我不知道是否应该使用 getter 和 setter 将这个 uniqueIddb 从一个类移动到另一个类。我要解决的问题是在类之间传递用户数据。
LoginController.java
package Login;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import sample.databaseHandler;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Random;
import java.util.ResourceBundle;
public class LoginController implements Initializable {
@FXML
private TextField email;
private String uniqueIddb;
Connection con = null;
public LoginController() {
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
private void closeStage(){
((Stage) email.getScene().getWindow()).close();
}
@FXML
private void loginUser(ActionEvent actionEvent) {
PreparedStatement stmt;
String userEmail = email.getText();
System.out.println(userEmail);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student Portal", "root", "");
System.out.println("connection has been made");
stmt = con.prepareStatement("SELECT Email,UniqueId FROM members WHERE Email = ? ");
stmt.setString(1, userEmail);
System.out.println(stmt);
ResultSet result = stmt.executeQuery();
while (result.next()) {
String emaildb = result.getString("Email");
*uniqueIddb = result.getString("UniqueId");*
if(userEmail.equals(emaildb) ){
closeStage();
Stage stage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("/Dashboard/dashboard.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
**setUniqueIddb(uniqueIddb);**
} else{
//pass an alert for wrong credentials
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Cant load Database", "Database Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public void setUniqueIddb(String uniqueIddb) {
this.uniqueIddb = uniqueIddb;
}
public String getUniqueId() {
return uniqueIddb;
}
}
DashboardController.java
package Dashboard;
import Login.LoginController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javax.swing.*;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DashboardController {
@FXML
private Label uniqueIdDisplay;
public DashboardController() {
unique();
}
public void unique(){
LoginController login = new LoginController();
**String uniqueID = login.getUniqueId();**
uniqueIdDisplay.setText(uniqueID);
}
@FXML
public void openGeneral(MouseEvent mouseEvent) {
try {
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/General/optionGeneral.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
public void openProfile(MouseEvent mouseEvent) {
try{
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/Profile/optionProfile.fxml"));
Scene scene =new Scene(root);
stage.setScene(scene);
stage.show();
}catch(IOException e){
e.printStackTrace();
}
}
@FXML
public void openPerformances(MouseEvent mouseEvent) {
try{
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/Performances/optionPerformances.fxml"));
Scene scene =new Scene(root);
stage.setScene(scene);
stage.show();
}catch(IOException e){
e.printStackTrace();
}
}
@FXML
public void openLectures(MouseEvent mouseEvent) {
try{
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/Lectures/optionLectures.fxml"));
Scene scene =new Scene(root);
stage.setScene(scene);
stage.show();
}catch(IOException e){
e.printStackTrace();
}
}
@FXML
private void enrollToCourse(MouseEvent mouseEvent) {
Stage stage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("enrollCourseDialog.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
【问题讨论】:
-
不要从登录控制器中检索 is,将其从登录控制器传递给仪表板控制器
-
我需要从登录控制器中检索它以及凭据查询。
-
正如我所说,不要那样做。相反,将 if from 登录控制器传递给仪表板控制器。您可以在登录控制器中获取对仪表板控制器的引用(因为这是您加载相应 FXML 的地方),但您无法从仪表板控制器中获取对登录控制器的引用。
-
我明白了,先生。这完美地工作并传递给仪表板控制器。谢谢。访问它会产生一个 nullPointException。
-
那你还有别的问题。阅读链接的问题(不是下面的答案,缺乏细节)。如果按照该问题的公认答案,您仍然得到一个空指针异常,那么 edit 您的问题将包含一个 minimal reproducible example 来演示该问题。