String cvalue = "";
Cookie cookies[] = request.getCookies();
if(cookies.length>=0){
for(int c=0;c<cookies.length;c++){
if(cookies[c].getName().equals("v"+id)){//换取当前ID的cookies名称
cvalue=cookies[c].getValue();break;
} }
}
if(!cvalue.equals(id)){//若没有则新建一个cookies
Cookie cookieid = new Cookie("v"+id,id);
cookieid.setMaxAge(5*60*1000);
response.addCookie(cookieid);
Stat s = new Stat();s.View(id);//当前ID 统计+1
}这是一个防止重复统计的代码,采用Cookie技术...可有时候正确,有时候提示:
The server encountered an internal error () that prevented it from fulfilling this request
java.lang.NullPointerException
请大家看看是那出错了,有时候可以正确显示!?

解决方案 »

  1.   

    if(cookies.length>=0)这句有错。
    如果Cookie cookies[] = request.getCookies();返回空,就会报java.lang.NullPointerException 
    你应该增加对空的判断。
      

  2.   

    cvalue=cookies[c].getValue();
    在cookies[c].getValue();取得到的值是NULL的时候,程序进行如下判断,则出现java.lang.NullPointerException 
    if(!cvalue.equals(id))
      

  3.   

    1L说的情况也会,判断的时候先做个判断是否为NULL的操作就成了