I am writing a jsp page that uses a scriptlet to call a method from a controller, which uses a method from a DAO, which performs a named query stored in a model corresponding to that DAO. I want this controller method to return an ArrayList which I can iterate through to retrieve its values to display on the page.
When the jsp gets to the method call, it gives me an NPE at the controller method, specifically on the line which would call the query method. What’s more vexing is that I call a similar method earlier on the page and get no exceptions there. My group has been using a Spring MVC framework with hibernate to handle our mapping.
I’ve tried to remove the non-essential parts of the code while leaving in things that might point to the solution. Let me know if you need more information and I will update as soon as possible.
(I plan to learn to avoid scriptlets in the future, as all the questions I’ve read so far tell me to avoid them.)
Here’s the code:
ReportingController method:
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 method calling a query:
@SuppressWarnings("unchecked")
@Transactional
public ArrayList<UserTime> getPunchesForUserAscendingByTime(User passedUser) {
Query loginQuery = em
.createNamedQuery("fetchAllUserTimesForUserAscend");
loginQuery.setParameter("user", passedUser);
return (ArrayList<UserTime>) loginQuery.getResultList();
}
Named Query within UserTime model:
@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>
The Null Pointer Exception:
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)
Your problem is that your useBean for reportControl is simply creating a brand new ReportingController, which does not have the UserDao injected.
What you are trying to do simply won’t work in Spring MVC, and nor should it. The objective of MVC is to separate the Model, View and Controller, but you are trying to create an instance of a controller inside a view.
Instead, the controller method you have that returns the JSP name should first call your weekWorkPrints method and put the returned List in the Model so that the JSP can iterate through it.