我在Spring MVC / Maven Web应用程序中的方法调用上遇到Null异常错误。我在方法中使用PagedListHolder从MySQL数据库中检索16个产品的页面中的产品。 Web应用程序在我的Live Centos 6 VPS上工作正常,但现在给我一个Null异常错误。完全相同的程序在我的Eclipse Mars测试环境中完美运行。
下面是一些Stack Trace和相关代码。希望有人可以为我解释这个问题。
谢谢和问候, Gus Hayes
堆栈跟踪
HTTP状态500 - 请求处理失败;嵌套异常是java.lang.NullPointerException
输入例外报告
消息请求处理失败;嵌套异常是java.lang.NullPointerException
说明服务器遇到内部错误,导致无法完成此请求。
例外
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是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)
根本原因
显示java.lang.NullPointerException com.thestoreyard.app.controllers.StoreController.clientsNavigateToPage(StoreController.java:77) sun.reflect.GeneratedMethodAccessor106.invoke(未知来源) 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)
商店控制器
// 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);
}
}