DAO层有这样一个方法:
      public String[] getOmcIpByCity(String city) throws DaoException {
// TODO Auto-generated method stub
Session session = getSession();
SQLQuery sqlQuery = session
.createSQLQuery("select distinct RELATED_OMC from  CUST_VLR where CITY='"
+ city + "'"); Iterator iter = sqlQuery.list().iterator();

if (iter.hasNext()) {
String[] s = new String[sqlQuery.list().size()];
;
int index = 0; while (iter.hasNext()) {
s[index] = iter.next().toString();
index++;
} releaseSession(session);
return s;
}
return null;
}此方法SQL的意思是通过CITY来取CITY下面对应的RELATED_OMC。 碰到了一个空指针的问题,例如:
    执行:select distinct RELATED_OMC from  CUST_VLR where CITY='泰州';
    数据库下查到的数据是:
                        1:null
                     2: 1.2.3.4
                     3: 5.3.6.9
   这样的话,“s[index] = iter.next().toString();”这句话就报空指针了。    请问有什么解决方法,各位帮忙看看。 

解决方案 »

  1.   

    你把这句放到try  catch语句如何:“s[index] = iter.next().toString();”
    try{
        s[index]=iter.next().toString();
    }catch(NullPointerException e){
        s[index]="";
    }
      

  2.   

    iter.next()为OBJECT对象从数据库出来的时候碰到值为null很正常
    s[index] = iter.next()==null?"":iter.next();
      

  3.   

    3 楼的  2次next() 那就不对了,,
      

  4.   

    select distinct RELATED_OMC from  CUST_VLR where CITY='泰州' and RELATED_OMC is not null