Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullPointerException when calling class from jsp

Status
Not open for further replies.

MalCarne

Technical User
Apr 1, 2005
70
US
I've got a very simple jsp page that calls a class method to return a ResultSet. The method calls another method in the same class to return a Connection. When run stand alone, everything within the class runs as expected.
But when called from the jsp, the Connection throws a NullPointerException.

The jsp:
Code:
	Employee emp = new Employee();
	ResultSet rs = null;
	String fname ="";
	String lname="";
	try
	{
		rs= emp.getEmployeeDetail(1003);
		if (rs.next())
		{
			fname = rs.getString("fname");
			lname= rs.getString("lname");
		}
		else 
		{
			System.out.println("Nothing here");
		}
	}
	catch (SQLException e)
	{
		e.printStackTrace();
	}

Error:

[9/13/07 11:17:56:693 EDT] 69dcac7f SystemErr R java.lang.NullPointerException
[9/13/07 11:17:56:693 EDT] 69dcac7f SystemErr R at user.Employee.getEmployeeDetail(Employee.java:94)
[9/13/07 11:17:56:693 EDT] 69dcac7f SystemErr R at org.apache.jsp._EmployeeDetails._jspService(_EmployeeDetails.java:83)
[9/13/07 11:17:56:693 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.jsp.runtime.HttpJspBase.service(HttpJspBase.java:89)
[9/13/07 11:17:56:693 EDT] 69dcac7f SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.jsp.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:344)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.serviceJspFile(JspServlet.java:669)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.service(JspServlet.java:767)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:983)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:564)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:200)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:119)
[9/13/07 11:17:56:708 EDT] 69dcac7f SystemErr R at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)[9/13/07 11:17:57:599 EDT] 69dcac7f WebGroup E SRVE0026E: [Servlet Error]-[]: java.lang.NullPointerException
at org.apache.jsp._EmployeeDetails._jspService(_EmployeeDetails.java:84)
at com.ibm.ws.webcontainer.jsp.runtime.HttpJspBase.service(HttpJspBase.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:344)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.serviceJspFile(JspServlet.java:669)
at com.ibm.ws.webcontainer.jsp.servlet.JspServlet.service(JspServlet.java:767)
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:983)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:564)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:200)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:119)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
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:618)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:672)


And the relevant parts of the Employee class:
Code:
public static Connection getCon()
	{
		Connection gConn=null;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
			gConn= DriverManager.getConnection("jdbc:odbc:enfodex","sa","moab");
		}
		catch (SQLException se)
		{
			se.printStackTrace();
		}
		catch(ClassNotFoundException cnf)
				{
					cnf.printStackTrace();
				}
		finally
		{

			return gConn;
		}
	}
	public static ResultSet getEmployeeDetail(int emp_id)
	{
		System.out.println("detail emp_id= " + emp_id);
		ResultSet rs = null;
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch(ClassNotFoundException cnf)
		{
			cnf.printStackTrace();
		}

		Connection con = getCon();
		try
		{
                //###Line 94
		  if (!con.isClosed())
			{
				CallableStatement cs = con.prepareCall("{call sp_getEmployeeDetails(?)}");
				cs.setInt(1,emp_id);
				cs.execute();
				rs = cs.getResultSet();
				if(rs.next())
				{
					System.out.println(rs.getString("fname"));
				}
			}
			else
			{
				System.out.println("no connection");
			}
		}
		catch(SQLException se)
		{
		se.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return rs;
	}

I shouldn't have to reference the connection from the jsp, should I? It seems that the connection is relevant to the getEmployeeDetail and not the calling jsp, no?

Any insight is greatly appreciated.
 
It looks like the Connection object con is null. If you changed
Code:
 if (!con.isClosed())
to
Code:
 if (con != null && !con.isClosed())
, you shouldn't get a null pointer exception. As far as why the connection object is null, I'm not so sure. Maybe you should declare gConn outside the method, and start the getCon() method by checking its status.
Code:
 if ((gConn == null) || (gConn.isClosed())) { ...
Are you sure these methods should be static?
 
Are you sure these methods should be static?

No, but the compiler screams about them not being static if I change that.

Changing to
Code:
 if (con != null && !con.isClosed())

bypasses the error, but doesn't really get me that far, as I'm kinda hoping to return some data here.

The part that confuses me the most here is that I can run the class standalone with no problem, but accessing it via jsp in the test environment causes it to error.
 
My bet is that you don't have the driver in your application server. You're catching exceptin when getting the connection but after that you hide them by returning the connection object, even it it's null

Cheers,
Dian
 
I see. The jdbcodbc driver is supposed to be in rt.jar, isn't it?
 
The compiler complains when a non-static method is referenced from a static context. I'm guessing thats what you're referring to. So, what else are you using that is static? I can tell you about the DataConnection Class I use. To use it, I create a new instance of the class, then call its open() method, which gets a connection among other things. Then I call executeQuery(String sql), and it returns a ResultSet. I do whatever with the resultSet, and then call the close() method. At this point I'm done with the class. Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top