[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

RE: Null Pointer exceptions in servlets since latest Apache/Jserv update



On Tue, 21 Mar 2000, LAVAU Michel wrote:

> At this point:
> 
> "rs = stmt.executeQuery("SELECT * FROM Table");"
> 
> you haven't yet initialized your stmt variable: "Statement           stmt;".
> You should first set 'stmt' to NULL when you declare it (in order to start
> with proper code), and moreover, you should get a valid 'Statement' instance
> with:
> "con.createStatement();", where 'con' is your "Connection" variable
> (properly instancied). Only now you can execute your query (but this does
> NOT mean you won't have a NullPointerException anymore: you still can get
> this kind of exception if your previous "con.createStatement();" statement
> failed).
I'm really sorry bothering you with this stupid mistake when I forgot
to open con.  Anyway it was kind of a cut-n-pasto, when I created the
test example.  The new appended file HalloNew.java which creates the
statement and does also a check, whether it worked or not.

I also included some debugging lines in the init() method.  Strangely
enough they dosn't find their way to the log files :-(!  I have no
clue why the initialisation fails.  I marked the line which is the
last one that is sended to the browser.

Any clue?

I expect problems in the Apache-Jserv-FreeTDS configuration but have
no idea what could be wrong.

Kind regards

              Andreas.
import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class HalloNew extends HttpServlet
			   //  implements SingleThreadModel
{ 
Connection          con;
ResultSet           rs;
Statement           stmt; 
ServletOutputStream out;

  public void init(ServletConfig conf) throws ServletException {
    super.init(conf);

    try {
      Class.forName("com.internetcds.jdbc.tds.Driver");
      //      Class.forName("com.internetcds.jdbc.tds.SybaseDriver");
    } catch (ClassNotFoundException e) { 
      System.err.println("CNF: " + e.getMessage());
      return;
    }

    String url      = "jdbc:freetds:sqlserver://sql03/Test";
    String user     = "TestUser" ;
    String password = "test";

    try {
      con = null;
      System.err.println("getConnection(" + url + ", " + user + ", " + password + ")");
      con   = DriverManager.getConnection(url, user, password);
      if ( con == null ) System.err.println("con = null :-(((");
      else               System.err.println("con != null :-)");
    } catch (SQLException se) {
      System.err.println("SQL: " + se.getMessage());
      return;
    } 

  }

  public void service (HttpServletRequest request,
                       HttpServletResponse res) 
    throws ServletException, IOException
  {
	   //            PrintWriter out;
    String title = "Example Apache JServ Servlet", query;

    res.setContentType("text/html");
    out  = res.getOutputStream();
    out.println("<HTML><HEAD><TITLE>");
    out.println(title);
    out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">");
    out.println("<H1>" + title + "</H1>");

    try {
      out.println("-2<br>");
      if ( con == null ) out.println("con = null :-(((");
      else               out.println("con != null :-)");
      //                              ^^^^^^^^^^^^^^^^
      // this is the last message in the browser
      // no entrys in the log files :-(
      stmt = con.createStatement(); 
      out.println("-1<br>");
    } catch (SQLException e) {
      out.println("Keine Verbindung " + e.getMessage());
      return ;
    } 
    out.println("0<br>");

    try {
      out.println("1<br>");
      rs    = stmt.executeQuery("SELECT * FROM Table");
      out.println("2<br>");
      if ( !rs.next() ) {
        out.println("3<br>");
        System.err.println("Error 4");
        return ;
      }
      out.println("4<br>");
      query = rs.getString("Column");
      out.println("5<br>");
      out.println(query);
      out.println("6<br>");
    } catch (SQLException se) {
      out.println("SQL: " + se.getMessage());
      return;
    } 
    out.println("<H2>Gratulation<br>");
    out.println("</BODY></HTML>");
    out.close();
  }
}
 

Reply to: