wild84
wild84
采纳率91.6%
2017-04-15 06:30

我的jsp程序用junit测试数据显示正确,但是用在jsp页面显示空指针异常

已采纳

程序如下:

 <%
    List<Emp> list=new EmpDao().findAll();
        for (Emp emp : list) {
            out.print(emp.getEmpno()+" "+emp.getEname()+" "+emp.getHireDate()+" "+emp.getSal()+"<br/>");
        }
     %>

显示异常:

 org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 35

32:     response.addCookie(c1);
33:     response.addCookie(c2);
34:     response.sendRedirect("cookie.jsp"); */
35:     List<Emp> list=new EmpDao().findAll();
36:         for (Emp emp : list) {
37:             out.print(emp.getEmpno()+" "+emp.getEname()+" "+emp.getHireDate()+" "+emp.getSal()+"<br/>");
38:         }


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


root cause
java.lang.NullPointerException
    dao.BaseDao.excuteFind(BaseDao.java:23)
    dao.EmpDao.findAll(EmpDao.java:14)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:153)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

检查无法获得连接,可是我用junit取得的连接是正常的,这怎么搞?

basedao程序

 package cn.anywhere.dao;
import java.io.*;
import java.sql.*;
import java.util.Properties;
import java.sql.DriverManager;

import cn.anywhere.util.DatabaseConnection;

public class BaseDAO {
    protected PreparedStatement pstmt = null;
    protected ResultSet rs = null;
    protected static Properties properties=null;
    protected static Connection connection = null;

    public ResultSet findAll(String sql){
        try{
            connection=DatabaseConnection.getConnection();
            pstmt=connection.prepareStatement(sql);
            rs=pstmt.executeQuery();
            return rs;
        } catch (SQLException e){
            e.printStackTrace();
        }
        return rs; 
    }
}

显示的异常是connection异常.我怀疑是我的oracle jdbc文件没放对位置.因为java程序我在biuld path里加载oracle7.jar文件做连接.但是JSP程序的oracle jdbc文件应该放哪个目录?

我的项目目录结构是这样的
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • wangyang1354 低调的洋仔 4年前

    BaseDao的第23行有问题,或者是看下EmpDao的第14行

    点赞 评论 复制链接分享
  • wangyang1354 低调的洋仔 4年前

    你直接在里面输出下new EmpDao()的结果

    点赞 评论 复制链接分享

相关推荐