java.lang.NullPointerException探讨

xuefeifangpei 2003-04-07 08:46:56
strSQL="select * FROM data where ";
if(!tousu_start_date1.trim().equals("") && !tousu_end_date1.trim().equals(""))
strSQL=strSQL+" tousu_date between #"+tousu_start_date1+"# and #"+tousu_end_date1+"# order by tousu_date desc";
if(!tousu_class1.trim().equals("") )
strSQL=strSQL+" and tousu_class like '%"+tousu_class1+"%' ";
if(!depart1.trim().equals("") )
strSQL=strSQL+" and depart = '"+depart1+"' ";
if(!handle1.trim().equals("") )
strSQL=strSQL+" and handle = '"+handle1+"' ";
if(!details1.trim().equals("") )
strSQL=strSQL+" and details like '%"+details1+"%' ";
if(!person1.trim().equals("") )
strSQL=strSQL+" and person = '"+person1+"' ";
我做了个查询怎么有时候报如下错误:如果把if条件语句换个顺序,在输入不通类别时它报同样下面的错

java.lang.NullPointerException
at _0002fsql_0005fok_0002ejspsql_0005fok_jsp_57._jspService(_0002fsql_0005fok_0002ejspsql_0005fok_jsp_57.java:210)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:184)
...全文
8 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xuefeifangpei 2003-04-08
我这样做了还是不行哦
tousu_end_date1=request.getParameter("tousu_end_date");
if (tousu_end_date1 ==null)
tousu_end_date1 ="";
depart1=request.getParameter("depart");
if (depart1 ==null)
depart1 ="";
details1=request.getParameter("details");
if (details1 ==null)
details1 ="";
handle1=request.getParameter("handle");
if (handle1 ==null)
handle1 ="";
person1=request.getParameter("person");
if (person1 ==null)
person1 ="";
tousu_class1=request.getParameter("tousu_class");
if (tousu_class1 ==null)
tousu_class1 ="";
strSQL="select * FROM data where ";

if(!tousu_class1.trim().equals(""))
strSQL=strSQL+" and tousu_class like '%"+tousu_class1+"%' ";
if(!depart1.trim().equals("") )
strSQL=strSQL+" depart = '"+depart1+"' ";
if( !handle1.trim().equals("") )
strSQL=strSQL+" and handle = '"+handle1+"' ";
if( !details1.trim().equals("") )
strSQL=strSQL+" and details like '%"+details1+"%' ";
if( !person1.trim().equals("") )
strSQL=strSQL+" and person = '"+person1+"' ";
if(!tousu_start_date1.trim().equals("") && !tousu_end_date1.trim().equals("") )
strSQL=strSQL+" and tousu_date between #"+tousu_start_date1+"# and #"+tousu_end_date1+"# order by tousu_date desc";
回复
dybine 2003-04-08
如果对象==null, trim()方法和equals("")方法都会抛出空指针异常
回复
凋零的老树 2003-04-08
原因很简单
tousu_start_date1 是null
所以执行
if(!tousu_start_date1.trim().equals("") && !tousu_end_date1.trim().equals("")) 肯定提示空指针错误
解决方法:
在前面加一句
if (tousu_start_date1 ==null)
tousu_start_date1 ="";
注意你的每一个做类似操作的变量都必须这样做
回复
xuefeifangpei 2003-04-08
这样写对么??但是还是不行哦??
strSQL="select * FROM data where ";
if( tousu_class1!=null && !tousu_class1.trim().equals("all"))
strSQL=strSQL+" tousu_class like '%"+tousu_class1+"%' ";
if(depart1!=null && !depart1.trim().equals("all") )
strSQL=strSQL+" and depart = '"+depart1+"' ";
if( handle1!=null && !handle1.trim().equals("all") )
strSQL=strSQL+" and handle = '"+handle1+"' ";
if( details1!=null && !details1.trim().equals("") )
strSQL=strSQL+" and details like '%"+details1+"%' ";
if( person1!=null && !person1.trim().equals("all") )
strSQL=strSQL+" and person = '"+person1+"' ";
if(!tousu_start_date1.trim().equals("") && !tousu_end_date1.trim().equals("") )
strSQL=strSQL+" and tousu_date between #"+tousu_start_date1+"# and #"+tousu_end_date1+"# order by tousu_date desc";
回复
qiri07 2003-04-07
http://expert.csdn.net/Expert/topic/1481/1481192.xml?temp=.7586328
回复
farawayMemory 2003-04-07
order by 从句应在 where 条件句之后

建议将以下if语句置后!
/*******************
if(!tousu_start_date1.trim().equals("") && !tousu_end_date1.trim().equals(""))
strSQL=strSQL+" tousu_date between #"+tousu_start_date1+"# and #"+tousu_end_date1+"# order by tousu_date desc";
/*******************
回复
Eraserpro 2003-04-07
同意楼上的观点,建议if第一个条件为xxxx!=null
回复
zez 2003-04-07
空指针...
...

你的equals 方法判断是不是 ""
但如果是null呢?

出错表示你引用了一个空的指针..
比如你根本没有查到信息而你引用它了或者查到信息但某一个字段是 null ,而你操作它了...
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-07 08:46
社区公告
暂无公告