2

I've retrieved data successfully from the db. One of the variables is uniqueiddb that I want to use in DashboardController. I need it as I'll have to query data for individual users in the database. But it's retrieved in the LoginController. I have to move it to the DashboardController. I have tried to use a setter to set the value in LoginController. When I use a getter in DashboardController, I get a NullPointException. It means the value is not set. I don't understand why. Someone please point to me where I'm going wrong. I don't know if I should use getters and setters to move this uniqueIddb from one class to the other. The problem that I want to solve is passing user data in between classes.

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();
    }
}

nzioker
  • 35
  • 5
  • 2
    Don’t retrieve the is from the login controller, pass it from the login controller to the dashboard controller – James_D Jun 20 '20 at 20:03
  • I needed to retrieve it from the login controller together with the query for credentials. – nzioker Jun 29 '20 at 13:36
  • 1
    As I said, don't do that. Instead, pass if *from* the login controller to the dashboard controller. You can get a reference to the dashboard controller in the login controller (because that's where you load the corresponding FXML), but you can't get a reference to the login controller from the dashboard controller. – James_D Jun 29 '20 at 13:43
  • I get you sir. That works perfectly and its passed to the dashboard controller. Thanks. Accessing it produces a nullPointException. – nzioker Jun 29 '20 at 15:30
  • 1
    Then you have something else wrong. Read the linked question (not the answer below, which lacks detail). If, following the accepted answer to that question, you still get a null pointer exception then [edit] your question to include a [mre] demonstrating the problem. – James_D Jun 29 '20 at 15:38
  • Alright. You've been of great help sir. Thank you so much. – nzioker Jun 29 '20 at 16:37

1 Answers1

3

In LoginController :

  String emaildb = result.getString("Email");
  uniqueIddb = result.getString("UniqueId");
  // to acsses Dashboard controller
  DashboardController controller = loader. < DashboardController > getController();
  //call the methode seID defined in DashboardController  to set the variable 
  controller.setID(uniqueIddb);

in your Dashboard controller Define seID Methode :

 class DashboardController {
      // Declaration of variable 
      int uniqueIddb;
      void initialize() {}
      // Methode setID
      void seID(String myID) {
      this.uniqueIddb = myID;
      }
  }
Asmoun
  • 1,417
  • 5
  • 20
  • 53
  • 1
    This is exactly what I wanted. Now its in the Dashboard. However, it raises a new question. When I assign it to a simple label for displaying, it tells me the uniqueId is always null. I want to use the uniqueId for displaying on the dashboard. How do I access it? – nzioker Jun 29 '20 at 13:35
  • 1
    Presumably you mean `this.uniqueIddb = myID`? – James_D Jun 29 '20 at 13:44
  • @James_D that's right thanks – Asmoun Jun 29 '20 at 14:13
  • @nzioker answer edited , check again if you can assign it to label – Asmoun Jun 29 '20 at 14:16
  • 1
    Yeah, I already saw that. Thank you. I dont know why its null yet it has been assigned – nzioker Jun 29 '20 at 15:31