jsp中的NullPointer异常

时间:2013-01-28 04:30:37

标签: sql oracle jsp nullpointerexception dao

org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet UserController threw exception
java.lang.NullPointerException
at com.nic.sample.dao.UserDAO.getAllUsers(UserDAO.java:69)
at com.nic.sample.controller.UserController.doGet(UserController.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:709)
at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:680)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:64)
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:388)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
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:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

运行我的应用程序时出现上述错误,使用dao将数据从数据库提取到jsp,我不知道为什么会出现此错误。需要帮忙。我的jsp和DAO代码在

之下

我的jsp文件listuser.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<%@page errorPage="error.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01    Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Show All Users</title>
</head>
<body>
<table border=1>
    <thead>
        <tr>
            <th>User Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>DOB</th>
            <th>Email</th>
            <th colspan=2>Action</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach items="${users}" var="user">
            <tr>
                <td><c:out value="${user.userid}" /></td>
                <td><c:out value="${user.firstName}" /></td>
                <td><c:out value="${user.lastName}" /></td>
                <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.dob}" /></td>
                <td><c:out value="${user.email}" /></td>
                <td><a href="UserController?action=edit&userId=<c:out value="${user.userId}"/>">Update</a></td>
                <td><a href="UserController?action=delete&userId=<c:out value="${user.userId}"/>">Delete</a></td>
            </tr>
        </c:forEach>
    </tbody>
</table>
<p><a href="UserController?action=insert">Add User</a></p>

我的UserDAO.java

package com.nic.sample.dao;

public class UserDAO {

private Connection connection;

public UserDAO() {
    connection = DbUtil.getConnection();
}

public void addUser(User user) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? )");
        // Parameters start with 1
        preparedStatement.setString(1, user.getFirstName());
        preparedStatement.setString(2, user.getLastName());
        preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
        preparedStatement.setString(4, user.getEmail());
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void deleteUser(int userId) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("delete from users where userId=?");
        // Parameters start with 1
        preparedStatement.setInt(1, userId);
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public void updateUser(User user) {
    try {
        PreparedStatement preparedStatement = connection
                .prepareStatement("update users set firstname=?, lastname=?, dob=?, email=?" +
                        "where userid=?");
        // Parameters start with 1
        preparedStatement.setString(1, user.getFirstName());
        preparedStatement.setString(2, user.getLastName());
        preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
        preparedStatement.setString(4, user.getEmail());
        preparedStatement.setInt(5, user.getUserid());
        preparedStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public List<User> getAllUsers() {
    List<User> users = new ArrayList<User>();
    try {
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("select * from users");
        while (rs.next()) {
            User user = new User();
            user.setUserid(rs.getInt("userid"));
            user.setFirstName(rs.getString("firstName"));
            user.setLastName(rs.getString("lastName"));
            user.setDob(rs.getDate("dob"));
            user.setEmail(rs.getString("email"));
            users.add(user);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return users;
}

public User getUserById(int userId) {
    User user = new User();
    try {
        PreparedStatement preparedStatement = connection.
                prepareStatement("select * from users where userid=?");
        preparedStatement.setInt(1, userId);
        ResultSet rs = preparedStatement.executeQuery();

        if (rs.next()) {
            user.setUserid(rs.getInt("userid"));
            user.setFirstName(rs.getString("firstName"));
            user.setLastName(rs.getString("lastName"));
            user.setDob(rs.getDate("dob"));
            user.setEmail(rs.getString("email"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return user;
}
}

请任何人帮助我,我真的被困在这里......

1 个答案:

答案 0 :(得分:0)

在你的getAllUsers方法中,connection.createStatement();可能是连接为空。

除此之外一切都很好。调试以确保......

最后也按照这个链接

http://www.vogella.com/articles/EclipseDebugging/article.html