I am getting a Null exception error on a method call in my Spring MVC/Maven web application. I am using PagedListHolder in a method to retrieve products in pages of 16 products from a MySQL database. The web application was working fine on my live Centos 6 VPS but now gives me a Null exception error. The exact same program is working perfectly on my Eclipse Mars test environment.
Below is some of the Stack Trace and the relevant code. Hopefully someone can shed some light on this issue for me.
Thanks and regards, Gus Hayes
Stack Trace
HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) net.sourceforge.pagesdialect.ExportFilter.doFilter(ExportFilter.java:52)
root cause
java.lang.NullPointerException com.thestoreyard.app.controllers.StoreController.clientsNavigateToPage(StoreController.java:77) sun.reflect.GeneratedMethodAccessor106.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
Store Controller
// List of Products by Category ID, paginated
@RequestMapping(value = "/category", method = RequestMethod.GET)
public String category(HttpServletRequest request, @RequestParam("id") Byte id, ModelMap mm) {
PagedListHolder pagedList = new PagedListHolder(productService.getByCategoryId(id));
pagedList.setPageSize(16);
request.getSession().setAttribute("productList", pagedList);
mm.put("productList", pagedList.getPageList());
mm.put("categoryList", categoryService.getAll());
mm.put("id", id);
return "store_catalog/products";
}
// The logic for pagination
@RequestMapping(value = "/categoryNav", method = RequestMethod.POST)
public String clientsNavigateToPage(HttpServletRequest request, ModelMap mm,
@RequestParam String action) {
PagedListHolder pagedList = (PagedListHolder) request.getSession().getAttribute("productList");
if (action.equals("next")) {
pagedList.nextPage();
} else if (action.equals("previous")) {
pagedList.previousPage();
}
mm.put("productList", pagedList.getPageList());
mm.put("categoryList", categoryService.getAll());
return "store_catalog/products";
}
// The product spec page by product ID
@RequestMapping(value = "/product", method = RequestMethod.GET)
public String product(@RequestParam("id") Integer id, ModelMap mm) {
mm.put("productSpec", productService.getById(id));
mm.put("categoryList", categoryService.getAll());
mm.put("id", id);
return "store_catalog/productspec";
}
products.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:pages="http://www.thymeleaf.org/pagesdialect"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="The Store Yard - Portlaoise, Co. Laois" />
<meta name="author" content="The Store Yard" />
<link href="resources/css/bootstrap.min.css" th:href="@{/resources/css/bootstrap.min.css}" rel="stylesheet" />
<link rel="stylesheet" type="text/css" media="all" href="resources/css/style.css" th:href="@{/resources/css/style.css}" />
<script type="text/javascript" src="resources/css/jquery.js" th:href="@{/resources/css/jquery.js}"></script>
<link type="text/css" rel="stylesheet" th:href="@{/resources/css/magiczoom.css}"/>
<script type="text/javascript" th:src="@{/resources/css/magiczoom.js}"></script>
<title>The Store Yard - Portlaoise, Co. Laois</title>
</head>
<body>
<div id="header">
<div id="social">
<a href="https://www.facebook.com/the-storeyard-Portlaoise-134807199880399/"><img src="resources/css/images/facebook.png" th:href="@{/resources/css/images/facebook.png}" alt="facebook" width="26" height="26" border="0" /></a>
<a href="https://twitter.com/thestoreyard"><img src="resources/css/images/twitter.png" th:href="@{/resources/css/images/twitter.png}" width="26" height="26" alt="twitter" /></a>
<a href="mailto:info@thestoreyard.ie"><img src="resources/css/images/email.png" th:href="@{/resources/css/images/email.png}" alt="email" width="26" height="26" border="0" /></a>
</div>
</div>
<ul class="dropdown"><!-- menu -->
<li><a href="home.html" th:href="@{/}">Home</a></li>
<li><a href="about.html">About Us</a></li>
<li><a href="categories.html" th:href="@{/categories}">Products</a></li>
<li><a href="store.html">Gallery</a>
<ul class="sub_menu">
<li><a href="store.html">Venue Hire</a></li>
<li><a href="media.html">In The Media</a></li>
<li><a href="music.html">Music @ The Store Yard</a></li>
<li><a href="films.html">Film Sets</a></li>
<li><a href="tynans.html">Tynans Restaurant</a></li>
</ul>
</li>
<!-- end submenu -->
<li><a href="location.html">Our Location</a></li>
<li><a href="contact.html">Contact Us</a></li>
</ul><!-- close menu -->
<div id="content">
<!-- content -->
<!-- <h1 th:class="${id} == ${category.id} ? 'active'" th:text="${#strings.toUpperCase(category.name)}"></h1> -->
<br></br>
<div class="col-xs-12 col-md-3" th:each="product : ${productList}">
<div class="thumbnail">
<a th:href="@{/product(id=${product.id})}"><img class="img-responsive"
th:src="@{|/resources/image_upload/products/${product.name}.jpg|}"></img></a>
<div class="caption">
<p>
<b>Name:</b> <span th:text="${product.name}"></span>
</p>
<p>
<b>Price:</b> <span th:text="|${product.price} Euro|"></span>
</p>
</div>
</div>
</div>
<form id="mainform" class="basic-grey" method="post" action="/categoryNav" th:action="@{/categoryNav}">
<input type="submit" name="action" value="previous" class="btn btn-info" />
<input type="submit" name="action" value="next" class="btn btn-info" />
</form>
</div>
<div id="sidebar"><!-- sidebar -->
<h3>Categories</h3>
<div class="navcontainer">
<ul class="nav nav-pills nav-stacked">
<li th:each="category : ${categoryList}" th:class="${id} == ${category.id} ? 'active'">
<a th:href="@{/category(id=${category.id})}" th:text="${#strings.toUpperCase(category.name)}"></a>
</li>
</ul>
</div>
</div><!-- end sidebar -->
<div id="footer">
<div align="center">
<p><a href="home.html" th:href="@{/}">Home</a> | <a href="about.html">About</a> | <a href="categories.html">Products</a> | <a href="store.html">Gallery</a> | <a href="location.html">Our Location</a> | <a href="contact.html">Contact</a> |</p>
<div class="footerimageleft"><img src="resources/css/images/irishtimes2.jpg" th:href="@{/resources/css/images/irishtimes2.jpg}" /></div>
<div class="footerimageright"><img src="resources/css/images/iada2.jpg" th:href="@{/resources/css/images/iada2.jpg}" /></div>
<p><h2>Office:+353 (0)57 86 80088</h2></p>
<br />
<p>©2016 - The Store Yard</p>
</div>
</div>
</body>
</html>
ProductService.java
package com.thestoreyard.app.service;
import com.thestoreyard.app.dao.ProductDao;
import com.thestoreyard.app.domain.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class ProductService {
@Resource
private ProductDao productDao;
public List<Product> getByCategoryId(Byte id) {
return productDao.findByCategoryId(id);
}
public Product getById(Integer id) {
return productDao.findOne(id);
}
}