由于没有明显原因,变量被设置为null

时间:2017-08-08 15:31:28

标签: java sql javafx null

我正在开发一个需要登录功能的项目,我几乎已经完成整个工作并正在进行最终检查。

因此,在handleLogin事件下,JavaFX GUI接收用户名和密码的输入,该用户名和密码首先存储在GertLogin Class中的userU和userP中。然后输入的用户名与DBMod的getData方法中数据库中存在的可用用户名交叉引用。此方法返回与用户输入的用户名对应的用户名和密码,并存储在GetLogin类的dbU和dbP中。那么应该发生的是比较两个密码,如果匹配则登录成功。

但是,在完成getData方法和调用checkData方法之间的某个地方,变量dbU和dbP都设置为null,当userU和userP都存储得很好时,我无法找到他们这样做或为什么这样做。

任何人都可以看到这到底发生了什么,为什么?

Home FXMLController:

package estorage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class HomeController implements Initializable {

    String username;
    String password;
    GetLogin login = new GetLogin(username, password);

    @FXML
    private Label registerPane;
    @FXML
    private Button btnLogin;
    @FXML
    private TextField txtUsername;
    @FXML
    private PasswordField txtPassword;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void handleRegisterScreen(MouseEvent event) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("Register.fxml"));

            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();

            stage.setScene(scene);
            stage.show();
            ((Node)(event.getSource())).getScene().getWindow().hide();
        }
        catch (IOException e) {
            Logger logger = Logger.getLogger(getClass().getName());
            logger.log(Level.SEVERE, "Failed to cretae new window", e);
        }
    }

    @FXML
    private void handleLogin(ActionEvent event) {
        username = txtUsername.getText();
        password = txtPassword.getText();
        login.userDetails(username, password);
        login.getDBDetails(username);
        login.getDetails();
        login.checkData();
    }

DBMod类:

package estorage;

import java.sql.*;

public class DBMod {
    static final String JDBC_DRIVER = "com.exasol.jdbc.EXADriver";
    static final String DB_URL = "jdbc:exa:192.168.56.101";
    static final String USER = "sys";
    static final String PASS = "exasol";

    //Add Entry to Database Method
    public void Add(String f, String l, String p, String u) {
        Connection con = null;
        PreparedStatement st = null;
        try {
            //Register JDBC driver
            Class.forName("com.exasol.jdbc.EXADriver");

            //Open connection
            System.out.println("Connecting to a selected database...");
            con = DriverManager.getConnection(DB_URL, USER, PASS);
            System.out.println("Connected database successfully...");

            //Execute a Query
            System.out.println("Adding details...");
            String insert = "INSERT INTO ES.USERS (firstname, lastname, username, password)" +
                         "VALUES (?, ?, ?, ?)";
            st = con.prepareStatement(insert);
            st.setString(1, f);
            st.setString(2, l);
            st.setString(3, u);
            st.setString(4, p);
            st.executeUpdate();


            //Extract all records & Update
            insert = "SELECT * FROM ES.USERS";
            ResultSet rs = st.executeQuery(insert);

             while(rs.next()) {
                //Retrive data by column name
                int id = rs.getInt("id");
                String firstName = rs.getString("firstname");
                String lastName = rs.getString("lastname");
                String username = rs.getString("username");
                String password = rs.getString("password");

                //Display values
                System.out.print("ID: " + id);
                System.out.print(", First Name: " + firstName);
                System.out.print(", Last Name: " + lastName);
                System.out.print(", Username: " + username);
                System.out.println(", Password: " + password);
            }
            rs.close();
        }

        catch (SQLException se){
            se.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(st != null)
                    con.close();              
            }
            catch(SQLException se) {

            }
            try {
                if(con != null)
                    con.close();
            }
            catch(SQLException se) {
                se.printStackTrace();
            }

        }

    }

    //Get data from Database method
    public void getData(String u) {
        String username = null;
        String password = null;
        GetLogin details = new GetLogin(username, password);
         Connection con = null;
         PreparedStatement st = null;
         try {
             //Register JDBC driver
             Class.forName("com.exasol.jdbc.EXADriver");

             //Open connection
             System.out.println("Connecting to a selected database...");
             con = DriverManager.getConnection(DB_URL, USER, PASS);
             System.out.println("Connected database successfully...");

             //Execute a Query
             System.out.println("Retrieving details...");
             String data = "SELECT USERNAME, PASSWORD FROM ES.USERS WHERE USERNAME = ?";
             st = con.prepareStatement(data);
             st.setString(1, u);
             ResultSet rs = st.executeQuery();

              while(rs.next()) {
                 //Retrive data by column name
                 username = rs.getString("USERNAME");
                 password = rs.getString("PASSWORD");

                 //Display values
                 System.out.println();
                 System.out.println();
                 System.out.println();
                 System.out.println();
                 System.out.println();
                 System.out.println("Send to method");
                details.DBDetails(username, password);
                System.out.println(username);
                System.out.println(password);
                System.out.println("End DB Method");
                System.out.println();
                System.out.println();
                System.out.println();
                System.out.println();


             }
             rs.close();
             System.out.println(username);
             System.out.println(password);
         }

         catch (SQLException se){
             se.printStackTrace();
         }
         catch (Exception e) {
             e.printStackTrace();
         }
         finally {
             try {
                 if(st != null)
                     con.close();              
             }
             catch(SQLException se) {

             }
             try {
                 if(con != null)
                     con.close();
             }
             catch(SQLException se) {
                 se.printStackTrace();
             }

         }

     }

GetLogin课程:

private String u;
private String p;
private String dbU;
private String dbP;
private String userU;
private String userP;

GetLogin(String username, String password){

    u = username;
    p = password;

}

public void getDBDetails (String u) {
    DBMod details = new DBMod();
    details.getData(u);
}

public void DBDetails(String username, String password) {
    dbU = username;
    dbP = password;
    System.out.println(dbU);
    System.out.println(dbP);

}

public void userDetails(String username, String password) {
    userU = username;
    userP = password;
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println(userU);
    System.out.println(userP);
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
}

public void checkData(){
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println(dbU);
    System.out.println(dbP);
    System.out.println(userU);
    System.out.println(userP);
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();

   if (userU.equals(dbU) && userP.equals(dbP)) {
       System.out.println("Sign in Successful!");
   }
   else {
       System.out.println("Incorrect username or password");
   }
} 

public void getDetails() {
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println(dbU + " " + dbP);
    System.out.println();
    System.out.println();
    System.out.println();

}

3 个答案:

答案 0 :(得分:3)

  

但是,在完成getData方法和调用checkData方法之间的某处,变量dbU和dbP都设置为null,我找不到位置或原因......

不,它们永远不会设置为与$array = [1 , [2,3] , 4 , [[5 , 6 , 7] , [8 ,9] , 10]]; $stripper = new ArrayStripper(); $stripper->strip($array); var_dump($stripper->get()); 不同的值,至少不适用于您正在调用null的实例。在checkData方法中,您可以创建getData类的其他实例,并为此新实例设置信息,而不是现有实例。

您可以通过将GetData实例作为参数传递而不是在GetData方法中创建新实例来解决此问题:

getData

void getData(String u, GetData details) {
    ...
}

但我建议重写程序

public void getDBDetails (String u) {
    DBMod details = new DBMod();
    details.getData(u, this);
}
boolean checkLoginData(String username, String password) {
    DBMod dbmod = new DBMod();
    return password != null && password.equals(dbmod.getData(username));
}

更简单,不是吗?

答案 1 :(得分:0)

没有完全检查你的代码,但是 查看代码的这一部分

 public void getData(String u) {
    String username = null;
    String password = null;
    GetLogin details = new GetLogin(username, password);

您正在使用用户名和密码启动该类为空

答案 2 :(得分:-3)

getDBDetails()定义为查找两个参数,但是对它的调用只发送一个。