即使我知道nationalID不为null,我的程序仍会给我一个空指针异常。它告诉我roster.delete(nationalID); line为空,但是当我在Netbeans中运行调试器时,它甚至都没有进入删除状态。我还发布了我发现相关的display.jsp部分和Del.jsp这是删除页面。

名册不为空

Servlet侦听器

public class FoamServletListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext sc = sce.getServletContext();

    final String path = sc.getRealPath("/WEB-INF/");
    Roster.initialize(path + "\\roster.dat");
    sc.log("Passed roster initialization");
    try {
        sc.setAttribute("roster", Roster.getInstance());
    } catch (RosterException ex) {
        Logger.getLogger(FoamServletListener.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    sce.getServletContext().removeAttribute("roster");
}


}

初始化方法

    Roster roster;

@Override
public void init() throws ServletException {
    super.init();

    roster = (Roster) getServletContext().getAttribute("roster");
}


Servlet

String url = "/Del.jsp";

    String action = request.getParameter("action");

    if (action == null) {
        action = "Delete";
    }

    switch (action) {
        case "QueryAthlete":
            nationalID = request.getParameter("nationalID");
            try {
                Athlete athlete = roster.find(nationalID);
                // attach athlete and forward to Delete.jsp
                request.setAttribute("athlete", athlete);
                url = "/Del.jsp";
            } catch (RosterException ex) {
                Logger.getLogger(EditServlet.class.getName()).log(Level.SEVERE, null, ex);
                // Logic to go back to display page
                url = "/Display.jsp";

            }

            break;
        case "Delete":

            // Get Parameters
            nationalID = request.getParameter("nationalID");

             try {
                roster.delete(nationalID);
                url = "/Del.jsp";
            } catch (RosterException ex) {
                Logger.getLogger(DeleteServlet.class.getName()).log(Level.SEVERE, null, ex);
                errMsg = "Cannot delete this Athlete.";
            }

            request.setAttribute("errMsg", errMsg);
            break;

        case "Cancel":
            url = "/Display.jsp";
            break;
    }
    request.getRequestDispatcher(url).forward(request, response);


Display.jsp

<td><a href="<c:url value ='/DeleteServlet'/>?action=QueryAthlete&nationalID=${athlete.nationalID}">Delete</a></td>


Del.jsp

<form name="name" action="<c:url value='/DeleteServlet'/>" method="POST">
        <table border="1" cellpadding="2">
            <thead></thead>
            <tbody>
                <tr>
                    <td>National ID</td>
                    <td>${athlete.nationalID}</td>
                </tr>
                <tr>
                    <td>First Name</td>
                    <td>${athlete.firstName}</td>
                </tr>
                <tr>
                    <td>Last Name</td>
                    <td>${athlete.lastName}</td>
                </tr>
                <tr>
                    <td>Date Of Birth</td>
                    <td>${athlete.dateOfBirth}</td>
                </tr>
                <tr>
                    <td>Are you sure you want to delete this Athlete?
                        <input type="submit" value="Delete" name="action" />
                        <input type="submit" value="Cancel" name="action" /></td>
                </tr>
            </tbody>
        </table>
                    <c:if test="${errMsg} != null" var="errMsg">
                    </c:if>
    </form>


这是StackTrace:

Info:   action = null
Warning:   StandardWrapperValve[DeleteServlet]: Servlet.service() for     servlet DeleteServlet threw exception
java.lang.NullPointerException
    at java.util.TreeMap.getEntry(TreeMap.java:347)
    at java.util.TreeMap.remove(TreeMap.java:603)
    at edu.saintpaul.csci2466.foam.roster.Roster.delete(Roster.java:143)
    at servlets.DeleteServlet.processRequest(DeleteServlet.java:83)
    at servlets.DeleteServlet.doPost(DeleteServlet.java:126)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at     org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt    erChain.java:344)
    at     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.    java:214)
    at     org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.jav    a:393)
    at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt    erChain.java:256)
    at     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.    java:214)
    at     org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:3    16)
    at    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:1    60)
    at     org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at     org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at     org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at     org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at     com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(Con    tainerMapper.java:459)
    at     com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java    :167)
    at     org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at     org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at         org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at      org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.ja    va:119)
    at     org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilter    Chain.java:284)
    at     org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFil    terChain.java:201)
    at     org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.    java:133)
    at     org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.    java:112)
    at     org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at     org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.    java:561)
    at     org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrate    gy.java:112)
    at     org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrat    egy.java:117)
    at     org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadI    OStrategy.java:56)
    at     org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run    (WorkerThreadIOStrategy.java:137)
    at     org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThread    Pool.java:565)
    at     org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPoo    l.java:545)
    at java.lang.Thread.run(Thread.java:745)

最佳答案

您得到的是空指针异常,不是becoz为null,而是becoz名册在roster.delete(nationalID);中为null

关于


当我在Netbeans中运行调试器时,它甚至都没有进入
删除大小写。


可能是您的代码未正确编译和部署,但异常是您从已部署的代码中实际得到的结果

关于java - 空指针异常roster.delete(nationalID);,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29236865/

10-10 17:33