Need help with NullPointerException in my servlet

Hi guys , I have a servlet called submit servlet ,which instantiates a DataAccessObject called LetterRequestDAO and stores a letter using that.

Here is the part of my code that does that.

letterList =null;

LtrDAO =new LetterRequestDAO("001");

LtrDAO.storeLetter(EID);

letterList = LtrDAO.getLetterList();

I have imported the package that LetterrequestDAO belongs to using

import com.letterrequest.DAOs.LetterRequestDAO;

I checked the LetterrequestDAO (I inserted a main method in it and ran it)and its not throwing any null pointerException .Its storing the Letter and retreiving the ArrayList with no problem .

The program highlights at

LtrDAO.storeLetter(EID); when the null pointer exception is thrown .What could be the problem ?Why is my servlet not able to instantiate LetterrequestDAO ?Any ideas .I'm stuck with this problem for a week now.I really appreciate any suggestions .

I can post the two programs if it wont annoy anyone .

Thanks a bunch ,

Kavita

[1176 byte] By [KRiveraa] at [2007-10-2 11:47:56]
# 1

If the servlet can't instantiate your variable, you'll get either a ClassNotFoundException or an OutOfMemoryException. So that's not the problem.

The problem is most likely inside the storeLetter() method, the method is throwing a NullPointerException. The logical first suggestion would be: is the EID parameter null by any chance?

gimbal2a at 2007-7-13 6:05:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

hi ,

No the EmpID is not null .

In the message , you will see that EmployeeID is 5.My connection object is not instantiating .Like I said I am getting the connection perfectly fine from LetterRequestDAO class.I dunno what's wrong.

Below is the error I get .

Kavita

[2/9/06 11:13:52:777 EST] 53d1373d WebGroupI SRVE0180I: [LTRWEB] [/LTRWEB] [Servlet.LOG]: SubmitServlet: init

[2/9/06 11:13:52:817 EST] 53d1373d SystemOutO Employee ID in else part of submit servlet is 5

[2/9/06 11:13:52:877 EST] 53d1373d SystemOutO Connection madenull

[2/9/06 11:13:52:877 EST] 53d1373d SystemOutO LtrDAO is instantiated

[2/9/06 11:13:52:877 EST] 53d1373d SystemOutO In storeLetter -in try block before setting String qs

[2/9/06 11:13:52:877 EST] 53d1373d SystemOutO In storeLetter -in try block after setting String qs

[2/9/06 11:13:52:877 EST] 53d1373d SystemOutO connection in LetterRequestDAO is null

[2/9/06 11:13:53:327 EST] 53d1373d WebGroupE SRVE0026E: [Servlet Error]-[SubmitServlet]: java.lang.NullPointerException

at com.letterrequest.DAOs.LetterRequestDAO.storeLetter(LetterRequestDAO.java:39)

at com.letterrequest.servlets.SubmitServlet.doPost(SubmitServlet.java:50)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)

at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)

at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)

at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)

at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)

at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1019)

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)

at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)

at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)

at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)

at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)

at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)

at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)

at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)

at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)

at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)

at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)

KRiveraa at 2007-7-13 6:05:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> at com.letterrequest.DAOs.LetterRequestDAO.storeLetter

> (LetterRequestDAO.java:39)

This means that a NullPointerException occurs inside the method LetterRequestDAO.storeLetter(), in file LetterRequestDAO.java on line 39.

Look at that line and figure out what is null on that line. It is either a null array in array access (nullArray[42]), member variable access (nullObject.ni = 7) or method call (nullObject.foo()).

sjasjaa at 2007-7-13 6:05:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

In my LetterRequestDAO constructor I am instantiating a Connection Factory.

public LetterRequestDAO(String ID)

{

ConnectionFactory cFactory = new ConnectionFactory();

this.con = cFactory.getConnection(ID);

System.out.println("Connection made"+ con);

}

This is instantiating and getting me the connection , if I insert a main() into LetterRequestDAO itself .

But if I am instantiating LetterRequestDAO from SumbitServlet

LtrDAO = new LetterRequestDAO("001");

if(LtrDAO!=null)

{

System.out.println("LtrDAO is instantiated");

}

LtrDAO.storeLetter(EID);

letterList = LtrDAO.getLetterList();

if (letterList != null)

{

HttpSession session = req.getSession(true);

req.setAttribute("LTRList", letterList);

session.setAttribute("LtrList", letterList);

RequestDispatcher dispatcher =

getServletContext().getRequestDispatcher(

"LetterRequestJSP.jsp");

for some reason I am not able to get Connection

That's why my program is failing at pst = con.prepareCall(qs); in storeLetter method .Ipublic void storeLetter(String EID)

{

try

{PreparedStatement pst = null;

System.out.println(

"In storeLetter -in try block before setting String qs");

String qs = "insert into LetterRequest(EmpID) values(?)";

System.out.println(

"In storeLetter -in try block after setting String qs");

System.out.println("connection in LetterRequestDAO is "+ con);

[b]pst = con.prepareCall(qs);[/b]

pst.setString(1, EID.trim());

System.out.println(

"In storeLetter -in try block after prepareCall");

boolean b = pst.execute();

System.out.println("After execute");

}

I am not able to understand WHY I am not able to get a connection from SubmitServlet . when I am able to get connection from other programs Any ideas please?

Thanks,

kavita

KRiveraa at 2007-7-13 6:05:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...