-2
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;
}
}

誰か助けてください、私は本当にここで立ち往生しています...

4

1 に答える 1

0

getAllUsers メソッドconnection.createStatement();で、接続が null である可能性があります。

それ以外はすべて問題ありません。デバッグして確認してください..

そして最後に、このリンクもたどってください

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

于 2013-01-28T08:25:30.133 に答える