首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

NullPointerException解决办法

2012-09-02 
NullPointerException在做对两个参数的查询时,有时会出现以下错误:javax.servlet.ServletException: java.

NullPointerException
在做对两个参数的查询时,有时会出现以下错误:
javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

然后说代码是错在这:

Java code
public String execute() throws Exception {        Map request = (Map) ActionContext.getContext().get("request");        if ( this.getUsername_c().equals("") || this.getCustomerid_c().equals("") )        {            request.put("list", service.findAll());        } else {            allRecorders = service.findUsersByUsernameCustomerid_count( this.getUsername_c() , this.getCustomerid_c() ) ;            request.put("currentPage",new Integer(currentPage)) ; //当前页            request.put("allRecorders",new Integer(allRecorders)) ;//总记录总数            request.put("list", service.findUsersByUsernameCustomerid( this.getUsername_c() , this.getCustomerid_c() ,this.getCurrentPage(),lineSize));        }        return SUCCESS;    }

说是其中
Java code
if ( this.getUsername_c().equals("") || this.getCustomerid_c().equals("") )

这句设计到空指针.想问下为什么?而且好像有时候正确有时候会报错

[解决办法]
this.getUsername_c().equals("") || this.getCustomerid_c().equals("") 
这行代码有问题。要么前面的没获取到值,要么后面的没有获取到值。
你可以把位置稍微调换一下。为:
("").equals(this.getUsername_c()) || ("").equals(this.getCustomerid_c())
这样可以避免NullPointException。

所谓的NullPointException就是当你引用了一个空的对象的时候,来进行这个空对象的操作就会报NullPointException
[解决办法]
if ("" == this.getUsername_c() || "" == this.getCustomerid_c())
[解决办法]
楼主代码格式有问题,判断控制正或者字符串控制的格式应该是这样的
Java code
if(null != str && !"".equal(str)){}
[解决办法]
"".equals(this.getUsername_c()) || "".equals(this.getCustomerid_c())

热点排行