zoukankan      html  css  js  c++  java
  • 遇到的NullPointerException

    遇到的NullpointerException

    为什么会出现空指针呢?

    向我下面的代码:

       public User getLoginUser(Connection con, String userCode) throws Exception {
            ResultSet rs = null;
            User user = new User();
            // 确保连接上了数据库
            if (con!=null){
                String sql = "select * from `smbms_user` where userCode=?";
                Object[] params = {userCode}; // 这个userCode为参数传进来的值,setObject
                rs = jdbcUtil.excute(con, sql, params);
            }
            while(rs.next()){
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("UserCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
            }
            jdbcUtil.release(null,null,rs);
            return user;
        }
    

    原因是我实例化了一个User ,在查不到userCode的情况下也就不能赋值了,所以返回的user指向未知;就空指针了

    改进:提示作用域并且都赋值为null。

    public User getLoginUser(Connection connection, String userCode)
    			throws Exception {
    		// TODO Auto-generated method stub
    		PreparedStatement pstm = null;
    		ResultSet rs = null;
    		User user = null;
    		if(null != connection){
    			String sql = "select * from smbms_user where userCode=?";
    			Object[] params = {userCode};
    			rs = BaseDao.execute(connection, pstm, rs, sql, params);
    			if(rs.next()){
    				user = new User();
    				user.setId(rs.getInt("id"));
    				user.setUserCode(rs.getString("userCode"));
    				user.setUserName(rs.getString("userName"));
    				user.setUserPassword(rs.getString("userPassword"));
    				user.setGender(rs.getInt("gender"));
    				user.setBirthday(rs.getDate("birthday"));
    				user.setPhone(rs.getString("phone"));
    				user.setAddress(rs.getString("address"));
    				user.setUserRole(rs.getInt("userRole"));
    				user.setCreatedBy(rs.getInt("createdBy"));
    				user.setCreationDate(rs.getTimestamp("creationDate"));
    				user.setModifyBy(rs.getInt("modifyBy"));
    				user.setModifyDate(rs.getTimestamp("modifyDate"));
    			}
    			BaseDao.closeResource(null, pstm, rs);
    		}
    		return user;
    
  • 相关阅读:
    Verdi 看波形常用快捷操作
    Tensorflow系列——Saver的用法
    Verilog-分频器
    (原创)列主元Gauss消去法的通用程序
    冒泡排序算法
    ADC 与实际电压值的关系
    直流耦合 交流耦合 耦合
    当前不会命中断点,源代码与原始版本不同
    示波器触发
    在头文件#pragma comment(lib,"glaux.lib");编译器提示waring C4081: 应输入“newline“
  • 原文地址:https://www.cnblogs.com/hujesse4/p/14610435.html
Copyright © 2011-2022 走看看