set()方法接收值但get()方法返回null

时间:2014-03-10 17:24:48

标签: java null setter getter

我正在创建一个登录页面,该页面从数据库中检索用户详细信息并存储在用户类中。 Set方法正确地接收值但是当我尝试使用Get方法访问细节时,它返回null。请参阅下面的代码片段,您能告诉我这里到底缺少什么。

从DB中检索详细信息并设置值:

class userdao:

user.setID(rs.getString("ID"));
user.setHashPassword(rs.getString("HashPassword"));
user.setAccessLevel(rs.getInt("AccessLevel"));
user.setLastDate(rs.getDate("LastAccessDate"));

注意:此后连接已关闭。

设置值:

班级用户:

public class User  
{
    private String ID;
    private String HashPassword;
    private int AccessLEVEL;
    private Date LastAccessDate;

    public User()
    {

    }

    public void setID(String uID) 
    {
        this.ID = uID;
    }    
    public String getID()
    {
        return this.ID;
    }
    public void setHashPassword(String uHash) 
    {
        this.HashPassword = uHash;
    }    
    public String getHashPassword()
    {
        return this.HashPassword;
    }
    public void setAccessLevel(int uALevel) 
    {
        this.AccessLEVEL = uALevel;
    }    
    public int getAccessLevel()
    {
        return this.AccessLEVEL;
    }
    public void setLastDate(Date uLDate) 
    {
        this.LastAccessDate = uLDate;
    }    
    public Date getLastDate()
    {
        return this.LastAccessDate;
    }
}

我正在从另一个接收null的类中访问get()值。

班级登录:

System.out.println(user.getID()); // returning null
System.out.println(user.getHashPassword()); // returning null
System.out.println(user.getAccessLevel()); // returning null
System.out.println(user.getLastDate()); // returning null

但是,当我在userdao类中创建变量(DB返回详细信息)并从登录类中调用它们时,值会正确返回。

你能帮我找出这段代码中的错误吗?

以下是login和userdao类的完整代码。

public class Login extends javax.swing.JFrame
{
    User user = new User();
    static String strUserID;
    static String strPwd;

    public Login()
    {
        super();
        initComponents();
        //JFrame jfrm = new JFrame("Login");       
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        PasswordEncrypt cpe = new PasswordEncrypt();
        User user = new User();
        UserDAO uDAO = new UserDAO();

        strUserID = jTextField1.getText();
        char[] pwd = jPasswordField1.getPassword();
        strPwd = new String(pwd);

        if (strUserID.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Please Enter The User ID");
            jTextField1.requestFocusInWindow();
        }
        else if (pwd.length == 0)
        {
            JOptionPane.showMessageDialog(null, "Please Enter The Password");
            jPasswordField1.requestFocusInWindow();
        }
        else
        {
            try
            {
                // Authenticate the User
                String strStoredPwd;
                boolean UserExists;

                uDAO.Authenticate(strUserID);

                strStoredPwd = uDAO.dhash;
                // The following lines are used only when the User class is returning a value
                System.out.println(user.getID());
                System.out.println(user.getHashPassword());
                System.out.println(user.getAccessLevel());
                System.out.println(user.getLastDate());                
                UserExists = cpe.validatePassword(strPwd, strStoredPwd);
            }
        }
    }
}

public class UserDAO
{
    User user = new User();
    public String dhash;
    private Connection connection;
    private Statement statement;

    // Authenticate Existing User
    public void Authenticate(String UserID) throws SQLException
    {
        String query = "SELECT * FROM USERS WHERE ID = '" + UserID + "'";
        ResultSet rs = null;

        try
        {
            connection = ConnectionFactory.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery(query);
            //Do some thing with rs

            if (rs.next())
            {
                user.setID(rs.getString("ID"));
                user.setHashPassword(rs.getString("HashPassword"));
                user.setAccessLevel(rs.getInt("AccessLevel"));
                user.setLastDate(rs.getDate("LastAccessDate"));                

                dhash = rs.getString("HashPassword");
            }
        }
        finally
        {
            DBUtil.close(rs);
            DBUtil.close(statement);
            DBUtil.close(connection);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您几乎肯定会使用两个不同的对象。以下是您可以测试的方法:

userdao中,添加:

System.out.format("userdao: %d%n", System.identityHashCode(user));

login中,添加相同的

System.out.format("login: %d%n", System.identityHashCode(user));

很难弄清楚究竟是什么问题,因为你没有粘贴相关代码,但这应该会让你走上正轨。

答案 1 :(得分:0)

将类Authenticate()的{​​{1}}方法的返回类型更新为

UserDAO //设置值

后返回用户对象

当您从类public User Authenticate(String UserID)调用上述方法时,现在它将返回已设置值的Login类对象,现在代码将更新如下:

User