关于jsp空指针异常,下面是全部代码

在debug里出错的地方已经注释标明,在这里谢谢大家了,在线等,很急的,有什么可以问,马上回!谢谢指导!

 package yang;
import java.sql.*;
public class JDBConnection {
    private final String dbDrive="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String url="jdbc:sqlserver://localhost:1433;DatabastName=yang";
    private final String userName="sa";
    private final String password="*******";
    private Connection con=null;
    public JDBConnection(){                                 //构造方法加载驱动
        try {
            Class.forName(dbDrive).newInstance();
        } catch (Exception ex) {
            System.out.println("数据库驱动加载失败:"+ex);
        }
    }
    public boolean creatConnection(){                       //数据库连接
        try {
            con = DriverManager.getConnection(url,userName,password);
            con.setAutoCommit(true);
        } catch (SQLException e) {
            System.out.println("数据库连接失败:"+e);
        }
        return true;
    }
    public boolean executeUpdate(String sql){               //对数据表增,删,改
        if(con==null){
            creatConnection();
        }
        try {
            Statement stmt = con.createStatement();
            int iCount = stmt.executeUpdate(sql);
            System.out.println("操作成功,所影响的记录数为"+ String.valueOf(iCount));
            return true;
        } catch (SQLException e) {
            return false;
        }       
    }
    public ResultSet executeQuery(String sql){              //对数据表查询
        ResultSet rs=null;                                               //在debug里是这里出错
        try {
            if(con==null){
                creatConnection();
            }
            Statement stmt = con.createStatement();
             rs = stmt.executeQuery(sql);
            return null;
        } catch (SQLException e) {
            //e.printStackTrace();

        }
        return rs ;

    }
    public void closeConnection(){                          //关闭数据库连接
        if(con==null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

下面是jsp代码

 <%@page import="java.sql.ResultSet"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <jsp:directive.page import="yang.JDBConnection"/>
   <%
    JDBConnection connection = new JDBConnection();
    String sql = "use yang select*from users";
    ResultSet rs = connection.executeQuery(sql);
   %>
  <body>
    <table>
    <!-- 以下代码显示用户信息 -->
    <tr>
            <td>id号</td><td>姓名</td>
            <td>性别</td><td>生日</td>
            <td>籍贯</td><td>电话</td>
        </tr>

    <%
            try{
        while(rs.next()){
        %>
        <tr>
            <td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td>
            <td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td>
            <td><%=rs.getString(5)%></td><td><%=rs.getString(6)%></td>
        </tr>
    <%
    }}catch(Exception e){
    e.printStackTrace();
    }
    finally{
    connection.closeConnection();
    } 
    %>
    </table>
    <form action="addUsers.jsp" id="form1" name="form1" method = "post">
        <table>
            <tr>
                <td>姓名:</td>
                <td><input name="name" type="text" id="name" size="20"/></td>
                <td>生日:</td>
                <td><input name="birthday" type="text" id="birthday" size="11"/></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input name="sex" type="radio" value="男" checked="checked"/>
                    男<input name="sex" type="radio" value="女"/>女</td>
                <td>电话:</td>
                <td><input name="tel" type="text" id="tel" size="11"/></td>
            </tr>
            <tr>
                <td>籍贯:</td>
                <td><input name="address" type="text" id="address"/></td>
                <td>&nbsp;</td>
            </tr>
        </table>
            <input type="submit" name="Submit" value="增加"\>
            <input type="reset" name="Submit2" value="重置"/>
    </form>
  </body>
</html>

这个是异常信息

 java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:114)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

图片说明

6个回答

public ResultSet executeQuery(String sql) {

ResultSet rs=null;

try {
if(con==null){
creatConnection();
}
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
//e.printStackTrace();

    }
    return rs ;

}




        <%
                    if (null != rs) {
                        try {
                            while (rs.next()) {
    %>
    <tr>
        <td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td>
        <td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td>
        <td><%=rs.getString(5)%></td><td><%=rs.getString(6)%></td>
    </tr>
<%
                    }
                } catch (Exception e){
                    e.printStackTrace();
                } finally {
                    connection.closeConnection();
                } 
            }
%>
q763497695
q763497695 真的非常感谢
3 年多之前 回复
wheat235
wheat235 回复q763497695: 没事
3 年多之前 回复
wheat235
wheat235 回复q763497695: 页面问题解决了,那你在debug 跟一下java类的代码, 看看creatConnection中链接创建成功没有, executeQuery返回的是啥
3 年多之前 回复
q763497695
q763497695 辛苦了
3 年多之前 回复
q763497695
q763497695 这样的话没报错,但是还是没数据
3 年多之前 回复

executeQuery 这个方法一直返回的都是null 你看看是不是这个原因
PS:最好把异常信息发出来瞧一瞧

q763497695
q763497695 回复wheat235: 把这个ResultSet rs=null;改成ResultSet rs;就报错未初始化
3 年多之前 回复
q763497695
q763497695 回复wheat235: 去掉return null;也没用,改成return rs;也是报异常
3 年多之前 回复
wheat235
wheat235 回复q763497695: 你修改以后的代码是咋样的?
3 年多之前 回复
wheat235
wheat235 回复q763497695: 明显没有数据 executeQuery 这个方法返回的就是null 你把return null; 去掉
3 年多之前 回复
q763497695
q763497695 <td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td> <td><%=rs.getString(5)%></td><td><%=rs.getString(6)%></td>始终输出不了里面的值,报空指针异常
3 年多之前 回复
q763497695
q763497695 回复wheat235: 一样的,都报异常
3 年多之前 回复
wheat235
wheat235 回复q763497695: while(rs.next()){ 这里加一个null判断 如果为空 就不执行while(rs.next()){ 循环
3 年多之前 回复
wheat235
wheat235 回复q763497695: rs = stmt.executeQuery(sql); return null; 这个成功就把结果返回
3 年多之前 回复
q763497695
q763497695 应该是EexecuteQuery这个方法的问题。但是怎么改正呢?
3 年多之前 回复
q763497695
q763497695 异常信息发出来了,你帮忙看看,谢谢
3 年多之前 回复

连接数据库应该没有成功吧?

q763497695
q763497695 回复Darren刘航: 改了,也还是这样
3 年多之前 回复
a305644149
Darrenhang 回复q763497695: 你把executeQuery这个方法返回ResultSet试试别返回null,
3 年多之前 回复
q763497695
q763497695 连接数据库成功了,没有报错
3 年多之前 回复

这个是异常信息

 java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:114)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

url databastName?

q763497695
q763497695 回复songplwh: 改过来还是一样的,不是这个的问题,不过谢谢啦
3 年多之前 回复
songplwh
songplwh 回复q763497695: databaseName
3 年多之前 回复
q763497695
q763497695 sqlserver2008,有什么不对的吗
3 年多之前 回复

你先把这条use yang select*from users SQL语句改为select*from users再试试

q763497695
q763497695 改过来就出错了。说“users”对象错误,只有加上“use yang”才没有报错,因为不加“use yang”就找不到users这个表,数据库,表都已经连接成功了,应该是其他代码的问题
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问