Click here to Skip to main content
15,305,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
WILL ANYONE PLEASE HELP ME IN THIS SITUATION..I AM ASKING THIS QUESTION FOR THE THIRD TIME.....PLEASE GIVE ME SOME SOLUTION.

I am telling u guys once again..that i have one index.jsp page with me and i have one Login.java(servlet) in my project...but when i build this project...its gets successfully build...with NO errors in it. But when i run this project it throws an exception called "java.lang.NullPointerException". I am not able to figure out as to what the ACTUALL PROBLEM is.......?????????????????

INDEX.JSP
<pre lang="xml"><%--
    Document   : index
    Created on : Aug 28, 2010, 8:04:10 AM
    Author     : Prateek
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Login Form </title>
    </head>
    <body>
            <form action="Login" method="POST"/>
        <br>
        <br>
   <center><h3> Please fill in the following information in order to continue with your registration</h3></center>
   <br>
   <br>
   <table border="2" bordercolor="black"  align="center"  cellpadding="5" cellspacing="2" width="450" height="300">
        <tr>
            <td ><center><font color="black"><b>First Name </b></font></center></td>
            <td><center><input type="text" name="fn"></center></td>
        </tr>
        <tr>
           <td><center><font color="black"><b>Last Name</b></font></center></td>
           <td><center><input type="text" name="ln"></center></td>
        </tr>
        <tr>
           <td><center><font color="black"><b>Address</b></font></center></td>
           <td><center><input type="text" name="address"></center></td>
        </tr>
        <tr>
           <td><center><font color="black"><b>Contact Number</b></font></center></td>
           <td><center><input type="text" name="cno"></center></td>
        </tr>
        <tr>
           <td><center><font color="black"><b>E-Mail ID</b></font></center></td>
           <td><center><input type="text" name="EMail"></center></td>
        </tr>
        <tr>
           <td><center><font color="black"><b>Registration Date</b></font></center></td>
           <td><center><input type="text" name="Rd"/></center></td>
        </tr>

    </table>
    </body>
<br>
<br>
<center><input type="submit" value="     Save       ">&nbsp;&nbsp;&nbsp;<input type="reset" value="     Reset        " onclick="location.reload"></center>
<</html>



LOGIN.JAVA
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

public class Login extends HttpServlet {
   
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
         String fn =request.getParameter("First Name");
         String ln =request.getParameter("Last Name");
         String address=request.getParameter("Address");
         String cno=request.getParameter("Contact Number");
         String EMail=request.getParameter("E-Mail ID");
         String Rd=request.getParameter("Registration Date");
        
       
                 
        try
        {
             if(fn.length()==0 && ln.length()==0 &&address.length()==0 && cno.length()==0 && EMail.length()==0 && Rd.length()==0)
            {
                String redirectURL = "error.jsp";
                response.sendRedirect(redirectURL);
            }
             else
             {
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 Connection c= DriverManager.getConnection("jdbc:odbc:Family","","");
                 PreparedStatement ps = c.prepareStatement("insert into Friends values(FirstName, LastName, Address, ContactNo, EMailID, RegistrationDate) values(?,?,?,?,?,?)");
                 ps.setString(1, fn);
                 ps.setString(2, ln);
                 ps.setString(3, address);
                 ps.setString(4, cno);
                 ps.setString(5, EMail);
                 ps.setString(6, Rd);
                 
                 ps.executeUpdate();
                 
                 String redirectURL = "success.jsp";
                 response.sendRedirect(redirectURL);
             }  
        }
             catch(Exception ec)
               {
                        out.print(ec);
               }
             finally 
               {
                out.close();
               }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    
}
Posted

1 solution

Unless you can show exactly where the NullPointerException (I am assuming you understand what this is) occurs, it is very difficult to offer much in the way of guidance. You will probably need to step through your code with the debugger until you find the offending variable.


[EDIT] Having looked again at your code I suspect you are using the wrong values in your request.getParameter(); calls. For example you have coded request.getParameter("First Name"); but I think the actual name of the variable is "fn", as specified in the JavaScript (of which I am no expert). [/EDIT]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900