运行servlet时发生java.lang.NullPointerException

时间:2015-04-13 11:00:37

标签: java jsp servlets

大家好我正在写一个简单的注册表来注册和该servlet。 我已将所有条目都包含在web.xml中。但我不知道为什么我得到" java.lang.NullPointerException"。每件事对我来说都很好,但我无法找出漏洞。请帮帮我。

index.jsp

<form action="InsertRecord" method="post">
    <fieldset> 
     <legend>
        <font face="Courier New" size="+1" 
                color="red">
                Student Registration
        </font>
    </legend> 
    <table>
        <tr>
            <td>Student Name &nbsp;&nbsp; :</td>
            <td><input type="text" name="name"></td>
        </tr>

        <tr>
            <td>Enrollment Number &nbsp;&nbsp; :</td>
            <td><input type="text" name="enrolmentNo"></td>
        </tr>

        <tr>
            <td>Program Name &nbsp;&nbsp; :</td>
            <td><input type="text" name="program"></td>
        </tr>

        <tr>
            <td>Gender &nbsp;&nbsp; : </td>
            <td><select Name="gender">
                <option value="male">Male</option>
                <option value="female">Female</option>      
                </select>
            </td>
        </tr>

        <tr>
            <td>Address &nbsp;&nbsp; :</td>
            <td><textarea rows="3" cols="20"></textarea> 
        </tr>
        <tr>
            <td colspan="2">
            <input type="submit" value="Registration"></td>
     </table>
</fieldset> 
</form>

的web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>student</display-name>
      <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
         </welcome-file-list>
      <servlet>
        <servlet-name>InsertRecord</servlet-name>
        <servlet-class>com.varun.InsertRecord</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>InsertRecord</servlet-name>
        <url-pattern>/InsertRecord</url-pattern>
      </servlet-mapping>
    </web-app>

InsertRecord.java 这是servlet

   package com.varun;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    public class InsertRecord extends HttpServlet {
        private static final long serialVersionUID = 1L;

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out=response.getWriter();

            String name=request.getParameter("name");
            String en=request.getParameter("enrolmentNo");
            String program=request.getParameter("program");
            String gender=request.getParameter("gender");
            String address=request.getParameter("address");

            int id=0;
            int enrol=0;

            if(name.equals("")||en.equals("")||program.equals("")
                    ||gender.equals("")||address.equals(""))
            {
                out.println("Please insert the valid data");
                RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
                rd.include(request, response);
            }
            else
            {
                out.println("good job");
                RequestDispatcher rd=request.getRequestDispatcher("/sucess.jsp");
                rd.include(request, response);
            }


        }

    }

4 个答案:

答案 0 :(得分:3)

字符串address为空。

String address=request.getParameter("address");

因为你的textarea没有名字:

<td><textarea rows="3" cols="20"></textarea> 

将其更改为

<td><textarea rows="3" cols="20" name ="address"></textarea> 

并且异常应该消失。

答案 1 :(得分:2)

您没有名称为address的任何参数,并且您正在请求request.getParameter("address");中获取空值。

在textarea中添加name属性,它将起作用

<textarea rows="3" cols="20" name="address"></textarea>

答案 2 :(得分:1)

下面的代码行可能是异常的原因:

    String address=request.getParameter("address");

因为您尚未使用属性名称地址定义任何html字段。

        <td><textarea rows="3" cols="20"></textarea> 

在textarea上面加上 name =“address”属性。 这可能会解决问题!

答案 3 :(得分:0)

更正您的.jsp代码:

<textarea rows="3" cols="20" name ="address"></textarea>