请大家帮我分析下 下面是空指针异常是怎样回事?
我是用数组提取数据库数据 ,连接如下:
public List List4(String sql){
List listRow = new ArrayList();
List listColumn = null;
try{
rs=stat.executeQuery(sql);
while(rs.next()){
listColumn = new ArrayList();
listColumn.add(rs.getString(1).trim());
listColumn.add(rs.getString(2).trim());
listColumn.add(rs.getString(3).trim());
listRow.add(listColumn);
}
return listRow;
}catch(SQLException e){
e.printStackTrace();
}
return listRow;
}
SQL语句如下:
String sqlwai= "select prodstep, sum(outstep) as alloutstep,sum(outtotal) as allouttotal from calpcs where PRODNO+prodname= ' "+number+ " ' group by prodstep order by prodstep ";
ArrayList text1=(ArrayList)conn.List4(sqlwai);
Iterator wai=text1.iterator();
for(int i1 =0; i1 <text1.size();i1++){
wai.hasNext();
List list1=(List) wai.next();
prodstep=(String)list1.get(0);
//fac=(String)list1.get(1);
outstep=(String)list1.get(1);
outtotal=(String)list1.get(2);
}
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
Connectionbean.Connectionbean.List4(Connectionbean.java:145)
myservlet.myservlet.fd(myservlet.java:89)
myservlet.myservlet.doPost(myservlet.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.
到底是怎样怎么回事呢? 请高手们指点下.在线等 谢谢 ~!!
------解决方法--------------------------------------------------------
while(rs.next()) //???不太明白这句话是否应该while(rs.hasNext())?
------解决方法--------------------------------------------------------
可能rs.getString(num)的时候,数据库里为空值,这个时候用trim()则会保NULL异常
------解决方法--------------------------------------------------------
改成这个试试
for(int i1 =0; i1 <text1.size();i1++){
if(wai.hasNext())
{
List list1=(List) wai.next();
prodstep=(String)list1.get(0);
//fac=(String)list1.get(1);
outstep=(String)list1.get(1);
outtotal=(String)list1.get(2);
}
}
------解决方法--------------------------------------------------------
可能是rs.getString(1)或者rs.getString(2)或者rs.getString(3)时,得到的值是空,再调用trim()时,就null异常了。
------解决方法--------------------------------------------------------