日期:2014-05-20  浏览次数:20810 次

在线等!!!一个既简单又复杂的问题
Java code
public static List<ReceivedBean> getReceived(){
        List<ReceivedBean> beans = null;
        ReceivedBean bean = null;
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            conn = DBPoolConn.getConn();
            String sql = "select * from db_customsms.dbo.tbl_smreceived";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                
                bean.setSm_id(rs.getInt("SM_ID"));
                bean.setOrgaddr(rs.getString("orgaddr"));
                bean.setDestaddr(rs.getString("destaddr"));
                bean.setSm_content(rs.getString("sm_content"));
                bean.setRecvtime(rs.getString("recvtime"));
                bean.setReserve1(rs.getInt("reserve1"));
                bean.setReserve2(rs.getString("reserve2"));
                bean.setSmtype(rs.getInt("smtype"));
                bean.setMessageid(rs.getString("messageid"));
                bean.setOrgaddrtype(rs.getInt("orgaddrtype"));
                bean.setActionid(rs.getInt("actionid"));
                bean.setActionreasonid(rs.getInt("actionreasonid"));
                bean.setServiceid(rs.getString("serviceid"));
                bean.setProtocoltype(rs.getString("protocoltype"));
                bean.setReaded(rs.getInt("readed"));
                bean.setDroped(rs.getInt("droped"));
                beans.add(bean);
                System.out.println(bean.getSm_id());
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBPoolConn.CloseAll(rs, pst, conn);
        }
        return beans;
    }


请问我这段代码哪里有问题呢?我实在是不知道,老是报这个错!!!
Java code
Exception in thread "main" java.lang.NullPointerException
    at com.chinagdn.services.SendMessage.getReceived(SendMessage.java:28)
    at com.chinagdn.services.SendMessage.main(SendMessage.java:82)



------解决方案--------------------
at com.chinagdn.services.SendMessage.getReceived(SendMessage.java:28)
出现空指针异常
 List<ReceivedBean> beans = null;
beans没有初始化
 beans.add(bean);空指针
------解决方案--------------------
Java code
while (rs.next()) {
    bean = new ReceivedBean(); //还没new,当然是null了
    bean.setSm_id(rs.getInt("SM_ID"));

------解决方案--------------------
bean 是空的。
bean = new ReceivedBean();
------解决方案--------------------
ReceivedBean bean = null; 没初始化…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………