Hi Andrew:
I think this is your problem: you are opening your connection in the init()!
init() is executed *just* once at servlet initialization time and *not* every
time a person posts (or gets)! So what is happening is your connection is opened
at init(), closed after the first doGet() and further doGets() don't have
connection to work with (hence a null pointer exception). So this is what you
should do:
Create and close the connection in the doGet()/doPost() and I believe your
troubles will be over. (You would do well to create a connection pool in the
init() and then get and release connections from the pool, but that's another
story..)
Geeta
andrew tollin wrote:
> Thank you Marco,
>
> I have attempted to close the statement along with the connection within the
> doGet method as follows:
> finally {
> //close the database connection.
> try {
> if (stmt !=null) stmt.close();
> if (con !=null) con.close();
> }
> catch (SQLException e) {}
> }
>
> I open the connection in the init() method as follows. I dont quite
> understand what my problem is! Help
>
> public void init(ServletConfig config)
> throws ServletException
> {
> super.init(config);
> try {
> // Loads the driver
> Class.forName("org.gjt.mm.mysql.Driver");
>
> // Get a Connection to the database
> con = DriverManager.getConnection (url);
> }
> catch (Exception E) {
> System.err.println("Unable to load driver.");
> E.printStackTrace();
> } // end catch
>
> ----- Original Message -----
> From: "Marco Trevisan" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, August 01, 2001 9:36 AM
> Subject: Re: Refresh servlet page problem
>
> > Looks like the first time the servlet is invoked it works OK.
> > Subsequent invocations mess up the connection (never
> > worked on mysql but I'd close and reopen the connection
> > or better use a connection pooling package, maybe stmt
> > isn't being closed).
> > When you open via the address bar I suspect you get the
> > cached HTML produced the first time.
> >
> > Marco
> >
> > ----- Original Message -----
> > From: andrew tollin
> > To: [EMAIL PROTECTED]
> > Sent: Tuesday, July 31, 2001 10:14 PM
> > Subject: Refresh servlet page problem
> >
> >
> >
> > Hello
> >
> > I have a problem with my servlets. I have created a number of servlets
> that
> > connect to a database, access a particular table, and insert the database
> > information into skeletal HTML. When I view my page on Tomcat port
> > 8090(changed from port 8080 due to DB probs) by entering
> > http://localhost:8090/servlet/UsefulAddresses for example, it loads with
> no
> > errors. However when I refresh the page I get this error:
> >
> > Error: 500: Location: /servlet/UsefulAddresses: Internal Servlet Error:
> > : java.lang.NullPointerException
> > at org.gjt.mm.mysql.Statement.executeQuery(Statement.java)
> > at org.gjt.mm.mysql.jdbc2.Statement.executeQuery(Statement.java:78)
> > at UsefulAddresses.doGet(UsefulAddresses.java:50)
> >
> > If i enter the servlet URL in the address bar again press return it
> returns
> > to the error free page! Every time I refresh this page or any other of my
> > servlets I get this error. Could anyone please tell me why this is. My
> > servlet code is the next email.
> >
> >
> ___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html