我的JSP总是java.lang.NullPointerException错误!10分钟内给分

xgchar 2003-03-31 04:20:43
我的JSP页面的代码如下:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="SqlType" scope="page" class="newsdb.Sqllink" />
<%
String sql="select * from newstype";
ResultSet rs = SqlType.executeQuery(sql);
while(rs.next()) {
String type =(String)rs.getString("type");
out.println(type);
}
rs.close();
SqlType.closeStmt();
SqlType.closeConn();
%>

我的Sqllink.java代码编译通过,代码如下:
package newsdb;
import java.sql.*;

public class Sqllink
{
// class field
String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
Connection conn = null;
Statement stmt = null;
ResultSet Rs = null ;

//class construction
public Sqllink(){
try{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException e){
System.err.println("mydb(): " + e.getMessage());
}
}

//class method executeQuery
public ResultSet executeQuery(String sql){
Rs = null ;
try{
conn = DriverManager.getConnection(url,"sa","password");
Statement stmt = conn.createStatement();
Rs = stmt.executeQuery(sql);
}

catch (SQLException ex)
{
System.err.println(ex.getMessage());
}

return Rs;
}


public void executeUpdate(String sql) {
stmt = null;
try {
conn = DriverManager.getConnection(url,"sa","password");
stmt = conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}

//calss method close database connection
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

}

可是JSP中总是出现错误:是那里出出了错误呢!谢谢呀
type Exception report

message Internal Server Error

description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
at newsdb.Sqllink.closeStmt(Sqllink.java:58)
at org.apache.jsp.test$jsp._jspService(test$jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:536)

...全文
11 点赞 收藏 21
写回复
21 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
凋零的老树 2003-03-31
public void finalize()
{
try
{
if (conn != null)
{
conn.close();
}
}
catch (SQLException exf)
{
System.err.println("error" + exf.getMessage());
}
}//在bean里加上这个就可以;注意稍作修改
回复
凋零的老树 2003-03-31
java能自动释放的
回复
foolbirds 2003-03-31
//rs.close();
SqlType.closeStmt();
SqlType.closeConn();
回复
xgchar 2003-03-31
回复人: kongxiangli(笑看红尘) ( ) 信誉:105 2003-03-31 16:45:00 得分:0


SqlType.closeStmt();
SqlType.closeConn();
这两句话去掉,按我刚才说的再试一下,只要你的BEAN编译通过,我觉得不会有问题啊?



谢谢你呀!的确是像你说的那样,但是去掉了以后就可以正常运行,数据库连接没有关闭,很影响系统资源
那我该如何关闭数据库连接呢!给了方法立刻给分,劳驾你了!真的很想知道为什么!谢谢你
回复
dybine 2003-03-31
修改closeStmt();closeConn()方法
在关闭Statement和Connection之前要判断它是否为空,
而且要采用Resultset、Statement、Connection的顺序来关闭。
回复
jcq 2003-03-31
to gary_shi(Gary Shi):编译应该是可以通过,不过,它只使用了executeQuery这个方法,所以它的class中定义的stmt肯定是null,当然在close就出错了呢。
回复
凋零的老树 2003-03-31
SqlType.closeStmt();
SqlType.closeConn();
这两句话去掉,按我刚才说的再试一下,只要你的BEAN编译通过,我觉得不会有问题啊?
回复
gary_shi 2003-03-31
jcq(疯子弟),代码没有错,肯定可以编译通过,你可以查一下变量的作用域

//class method executeQuery
public ResultSet executeQuery(String sql){
Rs = null ;
try{
conn = DriverManager.getConnection(url,"sa","password");
// ==这里你新定义了变量STMT,把类变量stmt给隐掉了,
//改成 stmt=conn.......
Statement stmt = conn.createStatement();
Rs = stmt.executeQuery(sql);
}

catch (SQLException ex)
{
System.err.println(ex.getMessage());
}

return Rs;
}
回复
Fred_Mark 2003-03-31
up
回复
xgchar 2003-03-31
回复人: moumouren(某某人) ( ) 信誉:100 2003-03-31 16:30:00 得分:0


public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

--------------------------------------->

public void closeStmt(){
try{
if(stmt!=null){
stmt.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}




谢谢你呀!你的办法我也试了的
没有成功,错误一样的
而且我数据库是SQL2000,TOMCAT猫没有任何错误的说明呀
你说是怎么回师呢?
回复
xgchar 2003-03-31
回复人: moumouren(某某人) ( ) 信誉:100 2003-03-31 16:30:00 得分:0


public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

--------------------------------------->

public void closeStmt(){
try{
if(stmt!=null){
stmt.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}






谢谢你呀!你的办法我也试了的,还是不可以
我的数据库是SQL2000
不知道是什么原应,而且我的TOMCAT没有说明有任何问题!
回复
zpwly 2003-03-31
报的是:空子针的错误!~就是你取出的值是空!还拿它作其他操作了!自己找找吧!
回复
gary_shi 2003-03-31
是你自己打的啊

public void closeStmt(){
try{
stmt.close();/// <<<=====出错的是这里
}
catch(SQLException e){
e.printStackTrace(); <<<<-=====打印的是这里
}
}
}

public void closeStmt(){
try{
stmt.close();//==> if (NULL!=stmt) stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}


回复
jcq 2003-03-31
你看看你的executeQuery这个method的实现吧,重新定义了stmt呢,怎么会编译通过呢。我的建议是你在closeStmt及closeConn这两个方法改成:
public void closeStmt(){
try{
if(stmt!=null) stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
if(conn!=null) conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
回复
yzxasd 2003-03-31
你程序里的变量sql在那啊

public ResultSet executeQuery(String sql){
Rs = null ;
try{
conn = DriverManager.getConnection(url,"sa","password");
Statement stmt = conn.createStatement();
Rs = stmt.executeQuery(sql);
}

catch (SQLException ex)
{
System.err.println(ex.getMessage());
}

return Rs;
}
回复
rainlin1 2003-03-31
分步执行一下,看看是哪儿出现的错误,肯定是你对没有初值的变量进行了操作引起的。
回复
xgchar 2003-03-31
回复人: kongxiangli(笑看红尘) ( ) 信誉:105 2003-03-31 16:25:00 得分:0


while(rs.next()) {
String type =(String)rs.getString("type");
out.println(type);
}
改成:
String type = "";
while(rs.next()) {
type = rs.getString("type");
if (type!=null)
out.println(type);
}



谢谢你!你的办法我试了的,还是不可以呀!
回复
moumouren 2003-03-31
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

--------------------------------------->

public void closeStmt(){
try{
if(stmt!=null){
stmt.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}

回复
凋零的老树 2003-03-31
while(rs.next()) {
String type =(String)rs.getString("type");
out.println(type);
}
改成:
String type = "";
while(rs.next()) {
type = rs.getString("type");
if (type!=null)
out.println(type);
}
回复
AllError 2003-03-31
sorry 收回刚才的话
回复
加载更多回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-03-31 04:20
社区公告
暂无公告