简体   繁体   English

struts2.3.12中的servletactioncontext null

[英]servletactioncontext null in struts2.3.12

for (Cookie cookie : ServletActionContext.getRequest().getCookies()) {

The above line is giving me 上面的线给我

java.lang.NullPointerException
        at interceptors.RemembermeInterceptor.intercept(RemembermeInterceptor.java:35)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

It used to work fine in struts2.1.8 I just upgraded the struts2 version in my POM.xml to 2.3.12 and it stopped working giving me the above error. 它曾经在struts2.1.8中正常工作,我刚刚将我的POM.xml中的struts2版本升级到2.3.12,它停止工作,给了我上面的错误。

There are two things that could cause a NullPointerException in this line: 有两件事可能会在此行中导致NullPointerException:

for (Cookie cookie : ServletActionContext.getRequest().getCookies()) {
  1. ServletActionContext.getRequest() will return null if it is called outside of an HTTP request. 如果在HTTP请求之外调用ServletActionContext.getRequest()它将返回null。 Since you are calling that line from an interceptor, that doesn't seem likely. 由于您是从拦截器呼叫该行,因此似乎不太可能。

  2. ServletActionContext.getRequest().getCookies() will return null if there are no cookies sent with the request. 如果没有随请求发送的cookie,则ServletActionContext.getRequest().getCookies()将返回null。 Attempting to iterate over a null collection or array will produce an NPE. 尝试遍历null集合或数组将产生NPE。

Try changing your code as follows: 尝试如下更改代码:

Cookie[] cookies = ServletActionContext.getRequest().getCookies();
if (cookies != null) {
  for (Cookie cookie : cookies) {
    // do something
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM