Results 1 to 4 of 4
  1. #1
    narayanarao is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default java.lang.NullPointerException RegistrationServlet.doPost(RegistrationServlet.jav a:3

    Please solve the error
    below is the servlet code

    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.sql.*;
    import java.io.*;
    public class LoginServlet extends HttpServlet
    {
    Connection con;
    PreparedStatement ps;
    public void init(ServletConfig config)
    {
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@loc alhost:1521:db", "system", "cad2010");
    ps = con.prepareStatement( "SELECT * FROM register WHERE username = ? AND password=?");
    }
    catch(Exception e){}
    }
    public void destroy()
    {
    try
    {
    ps.close();
    con.close();
    }
    catch (SQLException e)
    {
    }
    }
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
    String user=req.getParameter("username");
    String pwd=req.getParameter("password");
    boolean b=false;
    try
    {
    ps.setString(1,user);
    ps.setString(2,pwd);
    ResultSet rs=ps.executeQuery();
    b=rs.next();
    rs.close();
    }
    catch (SQLException e)
    { }
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<BODY BGCOLOR = red>");
    if(b){

    out.println("<H1> hello" +user+ "registered sucessfully</H1>");
    }
    else
    {
    out.println("<H2> The authentication failed</H2>");
    }
    out.println("</BODY>");
    out.println("</HTML>");
    out.close();
    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: java.lang.NullPointerException RegistrationServlet.doPost(RegistrationServlet.ja

    Moved from Advanced Java

    Please solve the error
    What error? The stack trace should provide all the information to trace where it is thrown, and you did not provide it.

    Two Comments:
    1)
    Java Code:
    catch (SQLException e)
    {
    }
    Never let exceptions fall through. At the very least print out the stack trace.

    2) Please wrap code in the code tags

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: java.lang.NullPointerException RegistrationServlet.doPost(RegistrationServlet.ja

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: java.lang.NullPointerException RegistrationServlet.doPost(RegistrationServlet.ja

    Follow Darryl's links. You need to use [code] tags [/code] otherwise your formatting disappears in the forum and your code is very hard to follow.

    Java Code:
    public class LoginServlet extends HttpServlet
    {
        Connection con;
        PreparedStatement ps;
    etc etc.
    }
    Do not give a servlet attributes.
    It is not threadsafe, and is a bug in waiting.
    Also follow doWhile's advice and printStackTrace() at the very least inside your catch blocks.
    Something is going wrong and you are simply ignoring what.
    I have my suspicions as to what exactly is up, but it would be guesswork.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. java.lang.NullPointerException
    By Pombi in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 03:12 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  5. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •