• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem while executing user defined method within a servlet

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi,i have a problem with servlet jdbc programming.I wrote a java class which returns database connection.it's working when we call it from ontother java class,
from a servlet it is showing NullPointerExcception.please tell me reason and suggetion to solve it.

public class LoginPageDataHandlerServlet extends HttpServlet
{
public ServletConfig servletconfig;
public ServletContext context;
public Connection con;
public Statement stmt;

public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
doPost(req,resp);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
RequestDispatcher dispatcher;
LoginPageDataBean loginData=new LoginPageDataBean();
loginData.setUserName(req.getParameter("UserName"));
loginData.setRegno(req.getParameter("Regno"));
loginData.setUserType(req.getParameter("UserType"));


dispatcher=req.getRequestDispatcher("LoginPageDataValidationServlet");
dispatcher.forward(req,resp);

}
}


LoginPageDataValidationServlet.java


package project.Model;

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

import project.DBA.*;
import project.Operations.*;
import project.Beans.FormBeans.*;

public class LoginPageDataValidationServlet extends HttpServlet
{
RequestDispatcher target;
int regno;
ResultSet rs;
Statement stmt;

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{




LoginPageDataBean data=(LoginPageDataBean)req.getAttribute("loginData");

String name=data.getUserName();
String Utype=data.getUserType();
String password=data.getRegno();


GeneralOperations validate=new GeneralOperations();


int isMember=validate.Validate(name,password,Utype);


}
}


package project.Operations;

import project.Operations.*;
import project.DBA.*;
import java.sql.*;
import java.util.*;

public class GeneralOperations extends access implements operations
{
/** Method takes three parameters they are username,password and type
of user and validate them .The validation based on excuting a query
if it acquire any result set then it is success if no reuslt ser means it failed and
return some number
*/

String name,password,user;
Connection con;
Statement stmt;
int regno=0;
ResultSet rs;

public void storeDetails(Collection store){}
public void EditDetails(Collections col){}
public Collection showDetails()
{
Collection col=null;
return col;
}


public int Validate(String name,String password,String user)
{
/**

There are Student or College or Administrato name and passwords are the resememble the user identification
upon successfull validation it returns regno of candidate

*/
try
{
stmt=getcreateStatement();
rs=stmt.executeQuery("select regno from "+user+"RegistrationTable where name='"+name+"' and password='"+password+"'");
rs.next();
int regno=rs.getInt(1);
return regno;
}
catch(SQLException e)
{
e.printStackTrace();
return 89;
}
}


}


package project.DBA;
import java.sql.*;
interface connection
{
public Connection getConnection();
}

public class access implements connection
{
public Connection con;
public Statement stmt;
public Connection getConnection()
{
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:XE","system","sairam");
System.out.println(con);
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
public Statement getcreateStatement()
{
try
{
Connection con=getConnection();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(stmt);
return stmt;
}
public Statement getprepareStatemet(String statement)
{
try
{
stmt=getConnection().prepareStatement(statement);
}
catch(Exception e)
{
e.printStackTrace();
}
return stmt;
}
}


error message like below


java.lang.NullPointerException
project.Operations.GeneralOperations.Validate(GeneralOperations.java:42)
project.Model.LoginPageDataValidationServlet.doPost(LoginPageDataValidationServlet.java:34)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
project.Model.LoginPageDataHandlerServlet.doPost(LoginPageDataHandlerServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


Validate Method is responsible for acquiring connection and validate the data but i am getting nullpointerexception while executing it only from a servlet ,but it is executing normally in onother java classes
,
 
Ranch Hand
Posts: 129
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gollusiva kumar wrote:
dispatcher=req.getRequestDispatcher("LoginPageDataValidationServlet");
dispatcher.forward(req,resp);




It should be

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gollusiva, can you please UseCodeTags? You can edit your post to add them. Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic