0

Hi all I am writing a simple registration form to register and for that servlet. I have included all the entries in web.xml. but i dont know why I am getting the "java.lang.NullPointerException". every thing looks fine to me but I am not able to figure out the loophole. please help me out with this.

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 This is 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);
            }


        }

    }
2
  • 2
    Include the stacktrace of the error. Commented Apr 13, 2015 at 11:01
  • 2
    When troubleshooting Exceptions please include the full stack trace in your question Commented Apr 13, 2015 at 11:01

4 Answers 4

3

the String address is null.

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

because your textarea has no name:

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

change it to

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

and the exception should gone away.

Sign up to request clarification or add additional context in comments.

Comments

2

You dont have any parameter with name address and you are fetching in request request.getParameter("address"); giving you a null value.

Add name attribute in textarea and it will work

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

4 Comments

That is not correct. The NullPointerExcpetion will be at address.equals("")). Not in request.getParameter("address")
@Jens sorry but I dont aggree the nullpointer will be on getParameter
@singhakash The getParameter call will return a null and not throw an Exception. Only when you try to use the value will you get a NullPointerException.
@singhakash I f you read the documentation you can see that it wil return null if the parameter is not in the request: Returns the value of a request parameter as a String, or null if the parameter does not exist
1

The below line of code can be the reason of exception :

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

as you have not defined any html field with the attribute name address.

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

include name="address" attribute in above textarea. This may solve the issue!

Comments

0

Correct your .jsp code :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.