Hi,
I have not been using servlets for very long. I have a book on servlet
programming and in that book is a simple database application. I am
currently working on something similar so I copied the code in the book and
tried to run it. The code compiles fine (i'm using JSDK2.0 and JDK2) but
when I start the servletrunner and open the web page that calls the servlet
code, the following error is brought up in the servletrunner window -
SimpleServlet: init
java.lang.NullPointerException:
at SimpleServlet.doGet(SimpleServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:499)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
at
sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.jav
a:165)
at sun.servlet.http.HttpServerHandler.handleConnection(Compiled Code)
at sun.servlet.http.HttpServerHandler.run(Compiled Code)
at java.lang.Thread.run(Thread.java:479)
Also, the browser returns this error -
HTTP 500 internal server error
I have checked my code against the book and they are the same and I have
now spent two days looking for this nullpointerexception. I am going crazy!!!
Could someone please point out my mistake? The code is attached.
Thankyou,
Alison Lindores
import java.sql.* ;
public class HTML
{
public static final int NORMAL = 0 ;
public static final int HEADING = 1 ;
public static final int LINE = 2 ;
public StringBuffer buffer ;
public HTML(String title)
{
buffer = new StringBuffer(4096) ;
this.buffer.append("<HTML><HEAD><TITLE>") ;
this.buffer.append(title) ;
this.buffer.append("</TITLE></HEAD><LINK REL=STYLESHEET HREF='mystyle.CSS'
TYPE='text/css'><BODY>") ;
} // end of constructor
public void add(int style, String text, boolean linebreak)
{
switch(style)
{
case NORMAL : this.buffer.append(text) ;
break ;
case HEADING : this.buffer.append("<H1>") ;
this.buffer.append(text) ;
this.buffer.append("</H1>") ;
break ;
case LINE : this.buffer.append("<HR>") ;
break ;
default : break ;
} // end of switch
if(linebreak)
buffer.append("<BR>") ;
} // end of add()
public String endPage()
{
this.buffer.append("</BODY></HTML>") ;
return this.buffer.toString() ;
} // end of getPage()
public boolean createTable(String labels[], ResultSet rs)
{
int cols = labels.length ;
this.buffer.append("<TABLE>") ;
this.buffer.append("<TR>") ;
for(int i = 0 ; i < cols; i++)
{
this.buffer.append("<TH ALIGN='CENTER'>") ;
this.buffer.append(labels[i]) ;
this.buffer.append("</TH>") ;
} // end of for loop
this.buffer.append("</TR>") ;
try
{
while(rs.next())
{
this.buffer.append("<TR>") ;
for(int i = 1; i <= cols; i++)
{
this.buffer.append("<TD>") ;
this.buffer.append(rs.getString(i)) ;
this.buffer.append("</TD>") ;
} // end of for loop
this.buffer.append("</TR>") ;
} // end of while loop
} // end of try
catch(SQLException sqle)
{
return false ;
} // end of catch
this.buffer.append("</TABLE>") ;
return true ;
} // end of addTable()
} // end of class HTML
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
import java.sql.* ;
public class SimpleServlet extends HttpServlet
{
Connection dbCon ;
public void init() throws ServletException
{
// Connect to the database
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
dbCon = DriverManager.getConnection("jdbc:odbc:rmfproject") ;
} // end of try
catch (ClassNotFoundException cnfe)
{
System.out.println("Database driver could not be found.") ;
System.out.println(cnfe.toString()) ;
throw new UnavailableException(this, "Driver not found") ;
} // end of catch
catch (SQLException sqle)
{
System.out.println("Error connecting to the database.") ;
System.out.println(sqle.toString()) ;
throw new UnavailableException(this, "Cannot connect to database") ;
} // end of catch
} // end of init()
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
ResultSet rs ;
Statement stmt ;
StringBuffer qry = new StringBuffer(1024) ;
res.setContentType("text/html") ;
PrintWriter out = res.getWriter() ;
// create the query
String key_word = req.getParameter("key_field") ;
if(key_word.length() > 0)
{
qry.append("SELECT * FROM Project WHERE") ;
qry.append(" Title LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" Overview LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" Location LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" Proponent LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" LeadName LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" IndustryName LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" StatusName LIKE '%") ;
qry.append(key_word) ;
qry.append("%' OR") ;
qry.append(" TypeName LIKE '%") ;
qry.append(key_word) ;
qry.append("%'") ; // what about requirements / LGAs?????
} // end of if
// execute query
try
{
stmt = dbCon.createStatement() ;
rs = stmt.executeQuery(qry.toString()) ;
} // end of try
catch(SQLException sqle)
{
res.sendError(res.SC_ACCEPTED, "The request was accepted but failed.") ;
return ;
} // end of catch
// output results
HTML h = new HTML("Simple Search Results") ;
h.add(HTML.HEADING, "This is a heading", true) ;
h.add(HTML.LINE, " ", false) ;
String labels[] = {"Title"} ;
if(!h.createTable(labels, rs))
{
res.sendError(res.SC_ACCEPTED, "The request was accepted but failed.") ;
return ;
} // end of if
out.println(h.endPage()) ;
out.close() ;
try
{
rs.close() ;
stmt.close() ;
} // end of try
catch(SQLException sqle)
{
res.sendError(res.SC_ACCEPTED, "The request was accepted but failed.") ;
return ;
} // end of catch
} // end of doGet()
public void destroy()
{
// disconnect from the database
try
{
dbCon.close() ;
} // end of try
catch(Exception e)
{
System.out.println("Database close failed") ;
System.out.println(e.toString()) ;
} // end of catch
} // end of destroy
} // end of class SimpleServlet
=============================================
3rd Year B Information Technology Student
Central Queensland University, Mackay Campus
Phone: (07) 4942 2627 or 0409 260 174
Email: [EMAIL PROTECTED]
=============================================