当前位置: 代码迷 >> Java Web开发 >> 帮忙看上 struts2上PrintWriter out = response.getWriter();出错
  详细解决方案

帮忙看上 struts2上PrintWriter out = response.getWriter();出错

热度:5768   发布时间:2013-02-25 21:11:39.0
帮忙看下 struts2下PrintWriter out = response.getWriter();出错
package com.sunmq.action;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;

public class CheckUserAction extends ActionSupport {
  private String type;
   
  public String execute() throws IOException {
  HttpServletResponse response=null;
  response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<script>alert('成功');window.location.reload();</script>");
  return SUCCESS;
  }

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}

出错如下
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.lang.NullPointerException
com.sunmq.action.CheckUserAction.execute(CheckUserAction.java:23)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)




------解决方案--------------------------------------------------------
HttpServletResponse response=null;//你都把它设为null

用 this.response 就可以获得到
------解决方案--------------------------------------------------------
HttpServletResponse response=null;
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();

你的response都还没有值。。。 你就设置setContentType,你说他能不报错不?
------解决方案--------------------------------------------------------
java.lang.NullPointerException
com.sunmq.action.CheckUserAction.execute(CheckUserAction.java:23)//这句空指针了,在23行。
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Mon Dec 10 16:56:22 CST 2018
  • 最近登录:Mon Dec 10 16:56:22 CST 2018
  • 最近登录:Mon Dec 10 16:56:22 CST 2018
  • 最近登录:Mon Dec 10 16:56:22 CST 2018
  • 最近登录:Mon Dec 10 16:56:22 CST 2018