This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Servlet help  RSS feed

 
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1084
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the exception is caused at line 64
that line is were the sql statement is

if that helps

thnx
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fOUND THE REASON FOR THE ERROR
THE DRIVER I WAS LOADING WAS A LITTLE WRITTEN WRONG.


THNX EVERYONE
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!