I am getting the following exception:
2002-10-28 13:56:55 StandardWrapperValve[jsp]: Servlet.service() for servlet
jsp threw exception
java.lang.NullPointerException
at star.DataAccessTag.doAfterBody(DataAccessTag.java:74)
Code is as follows: (exception line red)
package star;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public final class DataAccessTag implements BodyTag {
private PageContext pc = null;
private BodyContent body = null;
private StringBuffer sb = new StringBuffer();
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {}
public Tag getParent() { return null; }
public int doStartTag() throws JspException {
String path = "jdbc:odbc:NamesDSN";
String sql = "SELECT ID, first_name, last_name, Role FROM Names";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(path);
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
setVariables();
} catch (SQLException e) {
throw new JspTagException("An SQLException occurred!");
} catch (ClassNotFoundException e) {
throw new JspTagException("JDBC Driver not found.");
}
return EVAL_BODY_INCLUDE;
}
public void setBodyContent(BodyContent b) {
body = b;
}
public void doInitBody() throws JspException {}
private boolean setVariables() throws JspTagException {
try {
if (rs.next()) {
pc.setAttribute("ID", rs.getObject(1).toString());
pc.setAttribute("first_name", rs.getObject(2).toString());
pc.setAttribute("last_name", rs.getObject(3).toString());
pc.setAttribute("Role", rs.getObject(4).toString());
return true;
} else {
return false;
}
} catch (SQLException e) {
throw new JspTagException("SQLException occurred!");
}
}
public int doAfterBody() throws JspException {
try {
sb.append(body.getString());
body.clear();
} catch (IOException e) {
throw new JspTagException("Fatal IOException!");
}
if(setVariables()) {
return EVAL_BODY_INCLUDE;
}
try {
body.getEnclosingWriter().write(sb.toString());
} catch (IOException e) {
throw new JspTagException("Fatal IOException!");
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
return EVAL_PAGE;
}
public void release() {
pc = null;
body = null;
sb = null;
}
}
TIA,
Luis.
Luis A Q Araujo
(21) 2422-0168
(21) 9131-4989
[EMAIL PROTECTED]
[EMAIL PROTECTED]
[EMAIL PROTECTED]
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com