使用名为JPQL的查询调用方法时,.jsp处的java-NullPointerException

前端之家收集整理的这篇文章主要介绍了使用名为JPQL的查询调用方法时,.jsp处的java-NullPointerException 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在编写一个jsp页面,该页面使用scriptlet从控制器调用方法,该控制器使用DAO的方法,该方法执行存储在与该DAO相对应的模型中的命名查询.我希望此控制器方法返回一个ArrayList,我可以对其进行迭代以检索其值以显示页面上.
当jsp进入方法调用时,它在控制器方法处给了我一个NPE,特别是在调用查询方法的那一行上.更令人烦恼的是,我在页面的前面调用了类似的方法,并且在那里没有异常.我的小组一直在使用带有Hibernate的@L_403_0@ MVC框架来处理我们的映射.
我试图删除代码中不必要的部分,同时保留可能指向解决方案的内容.如果您需要更多信息,请告诉我,我们会尽快进行更新.
(我打算将来避免使用scriptlet,因为到目前为止我读过的所有问题都告诉我避免使用它们.)
这是代码
ReportingController方法

public ArrayList<String> weekWorkPrints(User user,CurrentWeek week) {
    ArrayList<UserTime> timeList = userTimeDao
            .getPunchesForUserAscendingByTime(user);
    ArrayList<String> prints = new ArrayList<String>();
    prints.add(utc.convertPeriodToPrint(utc.timeWorkedForADay(
            week.getSunday(),timeList)));
    return prints;
}

UserTimeDao方法调用查询

@SuppressWarnings("unchecked")
@Transactional
public ArrayList<UserTime> getPunchesForUserAscendingByTime(User passedUser) {

    Query loginQuery = em
            .createNamedQuery("fetchAllUserTimesForUserAscend");
    loginQuery.setParameter("user",passedUser);

    return (ArrayList<UserTime>) loginQuery.getResultList();
}

UserTime模型中的命名查询

    @NamedQueries({
    @NamedQuery(name = "fetchAllUserTimes",query = "SELECT ut FROM UserTime ut"),@NamedQuery(name = "fetchAllUserTimesForUserDesc",query = "SELECT ut FROM UserTime ut WHERE ut.user = :user ORDER BY ut.userClockIn DESC"),@NamedQuery(name = "fetchAllUserTimesForUserAscend",query = "SELECT ut FROM UserTime ut WHERE ut.user = :user ORDER BY ut.userClockIn")})

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="models.*"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="models.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="controllers.ReportingController"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="userDao" class="daos.UserDao" scope="request" />
<jsp:useBean id="reportControl" class="controllers.ReportingController"
scope="request" />
<jsp:useBean id="currentWeekControl"
class="controllers.CurrentWeekController" scope="session" />
<jsp:useBean id="userTimeDao" class="daos.UserTimeDao" scope="request" />
<html>
<head>

<link rel="stylesheet" type="text/css" href="css/global.css" />
<link rel="stylesheet" type="text/css" href="css/reporting.css" />
<Meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Administrator - Reporting</title>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h1>Administrator Home</h1>
        <%
            User loginUser = (User) session.getAttribute("loginUser");
        %>
        <p>
            Welcome,<%=loginUser.getUserName()%>!
        </p>
    </div>
<!-- ---- -->
                <table id="loggedTiMetable">
                    <%
                        CurrentWeek currentWeek = (CurrentWeek) session
                                .getAttribute("currentWeek");
                    %>
                    <!-- --- -->
                    <%
                        for (User iteratedUser : userDao.getAlphabeticalUsers()) {
                            ArrayList<String> weekPrintouts = reportControl.weekWorkPrints(
                                    iteratedUser,currentWeek);
                    %>
                    <tr>
                        <td><%=iteratedUser.getLastName()%>,<%=iteratedUser.getFirstName()%>
                        </td>
                        <td><%=weekPrintouts.get(0)%></td>
                    </tr>
                    <%
                        }
                    %>
                </table>

空指针异常:

2012-08-19 21:21:02.315::WARN:  Nested in org.apache.jasper.JasperException: java.lang.NullPointerException: java.lang.NullPointerException
    at controllers.ReportingController.weekWorkPrints(ReportingController.java:106)
    at org.apache.jsp.jsp_002dpages.Reporting_jsp._jspService(org.apache.jsp.jsp_002dpages.Reporting_jsp:196)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:470)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:285)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
最佳答案
您的问题是您的reportBean的useBean只是创建了一个全新的ReportingController,它没有注入UserDao.

您试图做的事情在Spring MVC中根本行不通,也不应该. MVC的目的是将模型,视图和控制器分开,但是您试图在视图内部创建控制器的实例.

相反,您拥有的返回JSP名称的控制器方法应首先调用您的weekWorkPrints方法,并将返回的List放入Model中,以便JSP可以对其进行迭代.

原文链接:/spring/531829.html

猜你在找的Spring相关文章