一下是书上的代码:<%@ page contentType="text/html;charset=gbk" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gbk" />
<title>动态响应contentType属性</title>
</head>
<body>
<center>
<p>动态响应contentType属性的案例</p>
<hr><br>
请选择你的保存的格式:
<form action="SetContentType.jsp" name="myForm" method="post">
<select name="format" id="format" >
<option value="text" >文本文件</option>
<option value="word">word文件</option>
<option value="excel">Excel文件</option>
</select>
<br><br>
<input type="submit" name="save" value="保存">
</form>
</center>
<%
String docType = request.getParameter("format");
if(docType.equals("text")) {
docType = "text/html";
} else if(docType.equals("word")) {
docType = "application/msword";
} else if(docType.equals("excel")) {
docType = "application/x-msexcel";
}
response.setContentType(docType);
%>
</body>
</html>在浏览器中运行时,异常提示如下:org.apache.jasper.JasperException: An exception occurred processing JSP page /SetContentType.jsp at line 2421:  </center>
22:  <%
23:  String docType = request.getParameter("format");
24:  if(docType.equals("text")) {
25:  docType = "text/html";
26:  } else if(docType.equals("word")) {
27:  docType = "application/msword";
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause java.lang.NullPointerException
org.apache.jsp.SetContentType_jsp._jspService(SetContentType_jsp.java:77)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)首先,谢谢所有的回答者!

解决方案 »

  1.   

    这个问题出错的原因很简单。
    String docType = request.getParameter("format");
    这行代码没有获取到预期的值,所以docType的值为空,所以在下面调用docType.equals("text")方法时报了空指针的异常。楼主可以考虑换个方法获取format的值。
      

  2.   

    java.lang.NullPointerException   NullPointerException.  Because docType is null You can write you  code as below  at line 24 . if (null!=docType&&docType.equals("text"))
      

  3.   

                        <select name="format" id="format" >
                        <option value="text" >文本文件</option>
                        <option value="word">word文件</option>
                        <option value="excel">Excel文件</option>
                        </select>String docType = request.getParameter("format");
    你确定这样能取到值?告诉你是取不到的。
      

  4.   

    2楼正解   if (null!=docType&&docType.equals("text")) 
    因为第一次进来docType为空  你又没做相应的判断  所以会抛异常的
    java代码最好写上一句response.setCharacterEncoding("gbk");
    不然会乱码的
      

  5.   

    是这样的,如果你把上面的代码写在一个页面的话,会出现(NullPointerException
    )即空指针异常。理论上讲,你的这段代码: <%
                String docType = request.getParameter("format");
                if(docType.equals("text")) {
                    docType = "text/html";
                } else if(docType.equals("word")) {
                        docType = "application/msword";
                    } else if(docType.equals("excel")) {
                            docType = "application/x-msexcel";
                        }
                response.setContentType(docType);
            %>应该是在SetContentType.jsp页面中的。你在代码恰加一句空指针的判断即可。

     if(docType!=null)
    {
           String docType = request.getParameter("format");
               if(docType.equals("text")) {
                    docType = "text/html";
                } else if(docType.equals("word")) {
                        docType = "application/msword";
                } else if(docType.equals("excel")) {
                            docType = "application/x-msexcel";
                       }
                response.setContentType(docType);
    }
      

  6.   

    楼上已经给出答案了,不过要提醒一句通常用equals时把常量放在左边
    如:
    if("a".equals(type)){
    //....
    }