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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I cant figure out this servlet error...

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
I am getting this error:
Code:
javax.servlet.ServletException: Error instantiating servlet class test.FetchEmployeeServlet
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
	org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
	java.lang.Thread.run(Thread.java:534)

this is the init portion of the servlet:
Code:
package test;

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

public class FetchEmployeeServlet extends HttpServlet {

	private final static String sql = "select * from employee where id = ?";
	private PreparedStatement statement = null;
	private ServletContext context;
	private Connection connection;
	private Context ctx;
	private DataSource ds;


	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		context = config.getServletContext();

		try{
			Context ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/BankDB");
			Connection connection = ds.getConnection();
			statement = connection.prepareStatement(sql);
		}
		catch(Exception e) {
			e.printStackTrace();
		}		

	}

Can anyone tell me what is going on? I am not familiar with reading error output yet. (but i am trying!!)

thanks
 

I don't think it's a problem with your reading the error output, it's just a horrible error message with no indication of *what* actually went wrong. :-( Nothing leaps off the page as to why it couldn't be instantiated, but I'm not an expert.

This is not so much Java advice, but general program debugging in general. Comment out as much code as you can in that servlet, then try instantiating it. Then start uncommenting code, like just the variable declarations. Keep commenting and uncommenting code until you find the offending lines.

I call it 'binary debugging', since I usually start by commenting out halves of code and recursing.
 
This error usually happen when servlet failed in the init() method. Even you try to catch the exception and print it out, it will not appear int the log file.

My guess is ds.getConnection() returns null, causing the following statement to throw NullPointerException. try comment out:

Connection connection = ds.getConnection();
statement = connection.prepareStatement(sql);

It possible get the servlet instantiated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top