GeeCON Prague 2014*
The moose likes Servlets and the fly likes Servlet help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet help" Watch "Servlet help" New topic
Author

Servlet help

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

i have a servlet that verify's data that is sent from a jsp page.
when i press the submit button the validate servlet runs
tomcat shows an error null pointer exception.any help?
the servlet code is shown beneath


import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidateData extends HttpServlet
{
Connection con;

public void init()
{
try
{
String conne = "jdbc:mysql://localhost/e_store";
Class.forName("org.gjt.mm.mmsql");
con = DriverManager.getConnection(conne);
}
catch (ClassNotFoundException e)
{
log("Couldn't find mm.mysql driver");
}

catch (SQLException e)
{
log("Sql Exception while connecting to MySQL",e);
}
}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String InvalidPassword = "t";
String EmailExists="t";
String exists = "f";

response.setContentType("text/html,charset=ISO-8859-7");
PrintWriter out = response.getWriter();

String FName = request.getParameter("Fname");
String LName = request.getParameter("LName");
String Address = request.getParameter("Address");
String Zipcode = request.getParameter("Zipcode");
String City = request.getParameter("City");
String Country = request.getParameter("Country");
String Telephone = request.getParameter("Telephone");
String Email = request.getParameter("e_mail");

String Username = request.getParameter("Username");
String Password = request.getParameter("Password");
String CPassword = request.getParameter("CPassword");

out.println("<head>");
out.println("<title>Validation Page</title>");
out.println("</head>");
out.println("<html>");
out.println("<body>");

if (CPassword!=Password)
InvalidPassword = "t";
else
InvalidPassword = "f";

try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select FName From customers Where E_Mail =" + "'Email';");

if (rs.next())
EmailExists = "t";
else
EmailExists = "f";
}

catch (SQLException e)
{
log("SQL Exception",e);
}



if (FName.equals(""))
out.println("<br> Please Insert First Name </br>");
if (LName.equals(""))
out.println("<br> Please Insert Last Name </br>");
if (Address.equals(""))
out.println("<br> Please Insert Address </br>");
if (Zipcode.equals(""))
out.println("<br> Please Insert ZipCode </br>");
if (Country.equals(""))
out.println("<br> Please Insert Country </br>");
if (City.equals(""))
out.println("<br> Please Insert City </br>");
if (Telephone.equals(""))
out.println("<br> Please Insert Telephone </br>");
if (InvalidPassword.equals("t"))
out.println("<br> Please Confirm Password </br>");
else
out.println("<br> Thank You For Registering </br>");

out.println("</body>");
out.println("</html>");
}
}



thnx in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Aris,
Can you post the stack trace or tell us what line is causing the NullPointerException?

Also, if you're going to post more than two or three lines of code, you should nest it inside UBB code tags so that your indenting is preserved.
This makes it easier for others to read it.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Aris ,

Just to add one more thing your variables(FName,LName,Address etc etc ) are assigned value after request.getParamter().If any of these parameter is null then (FName.equals("")) or any such check would cause a NullPointerException.

It is always a good practice whenever you comparing string compare them as
if("".equals(FName)) instead of if(FName.equals("")) because ("".equals(FName)) will never cause a NullPointerException



shailesh
[ April 21, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

the exception is caused at line 64
that line is were the sql statement is

if that helps

thnx
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're checking for SQL exceptions and logging them.
Did you look at these logs to verify that you have a connection?




If you try to call createStatement() on a null object you will see a NullPointerException..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



Also, that query is just two string literals pasted together.
Did you meant to write:

?
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
I want to add these two.

Check your Driver Class is loaded properly. Check whehter Connection object is not null before you use it

Regards


Sarath Mohan
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

this is the tomcat stack trace


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
at ValidateData.doPost(ValidateData.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410


log("Couldn't find mm.mysql driver");
log("Sql Exception while connecting to MySQL",e);


Aris,
You're logging SQL exceptions with some kind of logger.
Read that file and see if it's blowing up when you try to get a connection.

If so, then it makes perfect sense that line 64 would cause an NPE.


If you can't find that log file, add some new printStackTrace lines up in the code where you're trying to get the connection.






The errors you're getting on line 64 are most likely the result of errors being suppressed earlier in the method.
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

fOUND THE REASON FOR THE ERROR
THE DRIVER I WAS LOADING WAS A LITTLE WRITTEN WRONG.


THNX EVERYONE
 
GeeCON Prague 2014
 
subject: Servlet help