搞了好久了.头都大了.各位帮帮忙吧.
错误:
2012-4-6 19:51:07 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at mybean.StyleList.getBuffer(StyleList.java:36)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:127)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.ajp.AjpAprProcessor.process(AjpAprProcessor.java:427)
at org.apache.coyote.ajp.AjpAprProtocol$AjpConnectionHandler.process(AjpAprProtocol.java:384)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527)
at java.lang.Thread.run(Thread.java:619)
styleList.java:
- Java code
/* * 网站JAVABEAN * 所有帖子分类列表. * 使用数据源连接. * 前后台共用JAVABEAN. */package mybean;import java.sql.*;import mybean.DBConnSource;public class StyleList { private String tableName; private Statement stmt; private ResultSet rs; private DBConnSource dbc; public StyleList(){} public void setTableName(String n){ this.tableName=n; } public StringBuffer getBuffer(){ StringBuffer buffer=new StringBuffer(); dbc=new DBConnSource("jdbc/myweb"); try{ stmt=dbc.getStmt(); }catch(Exception e){ System.out.print("不能连接到数据源"+e.toString()); } try{ //这就是36行; String strSql="SELECT * FROM "+tableName; rs = stmt.executeQuery(strSql); while(rs.next()){ buffer.append("<a href='style.jsp?style="+rs.getString("style")+"' target=_blank>"+rs.getString("style")+"</a>"); buffer.append(" "); } }catch(SQLException e){ System.out.print("结果集rs获取出错"+e.toString()); }finally{ try{ //关闭 ResultSet rs. if(rs!=null){ rs.close(); } }catch(SQLException ex){ System.out.print("关闭rs出错"+ex.toString()); }finally{ try{ // 关闭 Statement stmt. if(stmt!=null){ stmt.close(); } }catch(SQLException ex){ System.out.print("关闭stmt出错"+ex.toString()); } } dbc.DBclose(); //关闭 DBConnsource dbc; } return buffer; }}
谢谢!
[解决办法]
java.lang.NullPointerException
at mybean.StyleList.getBuffer(StyleList.java:36)
估计你36行算错了吧?应该是stmt == null 吧。
你是不是没注意之前已经出错误信息了??
System.out.print("不能连接到数据源"+e.toString());
这行字没看到输出么?
[解决办法]
//帮你修改了一下。
- Java code
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;public class DBConnSource{ public static Connection getConnection(String dsName){ Connection conn = null; Context initCtx; try { initCtx = new InitialContext(); Context ctx =(Context)initCtx.lookup("java:comp/env"); DataSource ds =(DataSource)ctx.lookup(dsName); conn = ds.getConnection(); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static Statement getStmt(Connection conn){ Statement stmt = null; try { stmt=conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static PreparedStatement getPstmt(Connection conn,String sql){ PreparedStatement pstmt = null; try { pstmt=conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static void close(Statement stmt){ if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(PreparedStatement pstmt){ if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection conn){ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }}