几周前我开始学习 Java,来自 PHP,没有其他语言背景。我习惯只使用数组()...
当我将我的类从 HashMap 更改为 LinkedHashMap 时,我得到了一个空指针异常,
使用 JDK 1.6
类 LinkedHashMap - 来自 Oracle Oracle JDK 1.6 参考
此类提供所有可选的 Map 操作,并允许 null 元素。与 HashMap 一样,它为基本操作(添加、包含和删除)提供恒定时间性能,假设哈希函数在桶中正确地分散元素。由于维护链表的额外费用,性能可能略低于 HashMap,但有一个例外:迭代 LinkedHashMap 的集合视图所需的时间与映射的大小成正比,而不管其容量如何. HashMap 的迭代可能更昂贵,需要的时间与其容量成正比。
不工作
@Component("blMenu")
public class MenuProcessor extends AbstractModelVariableModifierProcessor {
public List<Category> topLevelCategories;
public LinkedHashMap<Category, LinkedHashMap<Integer, List<Category>>> navigationMenu = new LinkedHashMap<Category, LinkedHashMap<Integer, List<Category>>>();
public List<Category> Categories;
public int chunkSize = 5;
public int subCategoriesSize;
public String resultVar;
/**
* Sets the name of this processor to be used in Thymeleaf template
*/
public MenuProcessor() {
super("menu");
}
@Override
public int getPrecedence() {
return 10000;
}
@Override
protected void modifyModelAttributes(Arguments arguments, Element element) {
CatalogService catalogService = ProcessorUtils.getCatalogService(arguments);
resultVar = element.getAttributeValue("resultVar");
String parentCategory = element.getAttributeValue("parentCategory");
String unparsedMaxResults = element.getAttributeValue("maxResults");
// TODO: Potentially write an algorithm that will pick the minimum depth category
// instead of the first category in the list
List<Category> categories = catalogService.findCategoriesByName(parentCategory);
if (categories != null && categories.size() > 0) {
// gets child categories in order ONLY if they are in the xref table and active
List<Category> subcategories = categories.get(0).getChildCategories();
if (subcategories != null && !subcategories.isEmpty()) {
if (StringUtils.isNotEmpty(unparsedMaxResults)) {
int maxResults = Integer.parseInt(unparsedMaxResults);
if (subcategories.size() > maxResults) {
subcategories = subcategories.subList(0, maxResults);
}
}
}
topLevelCategories = subcategories;
buildMenu(arguments, catalogService);
}
}
public void buildMenu(Arguments arguments, CatalogService catalogService)
{
LinkedHashMap<Integer, List<Category>> chunkSubs;
for(Category topLvlCat : topLevelCategories)
{
Categories = catalogService.findCategoriesByName(topLvlCat.getName());
List<Category> subCategories = Categories.get(0).getChildCategories();
if(subCategories != null && !subCategories.isEmpty())
{
subCategoriesSize = subCategories.size();
chunkSubs = chunkSubCategories(subCategories);
navigationMenu.put(topLvlCat, chunkSubs);
}
else
{
navigationMenu.put(topLvlCat, null);
}
}
addToModel(arguments, resultVar, navigationMenu);
}
public LinkedHashMap<Integer, List<Category>> chunkSubCategories(List<Category> subCategories)
{
List<Category> chunkedCategories;
LinkedHashMap<Integer, List<Category>> subCategoriesChunked = new LinkedHashMap<Integer, List<Category>>();
int iLoop = 0;
int start = 0;
int end = chunkSize - 1;
int catSize = subCategoriesSize-1;
if(subCategoriesSize > 1){
do
{
if(end < catSize)
{
chunkedCategories = subCategories.subList(start, end);
subCategoriesChunked.put(iLoop, chunkedCategories);
}
else
{
end = end - (end-catSize);
chunkedCategories = subCategories.subList(start, end);
subCategoriesChunked.put(iLoop, chunkedCategories);
}
iLoop = iLoop + 5;
start = start + chunkSize;
end = end + chunkSize;
}while(subCategoriesSize > iLoop);
}
return subCategoriesChunked;
}
}
但是将所有 LinkHashMaps 更改为 HashMaps 就可以了。
堆栈跟踪
2013 年 3 月 20 日下午 12:37:46 org.apache.catalina.core.StandardWrapperValve 调用严重:servlet [mycompany] 在路径 [/site] 的上下文中的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:在执行处理器 'com.mycompany.processor.MenuProcessor' (layout/partials/nav:8)] 时出错,根本原因是 com.mycompany.processor 处的 java.lang.NullPointerException。 MenuProcessor.buildMenu(MenuProcessor.java:106) at com.mycompany.processor.MenuProcessor.modifyModelAttributes(MenuProcessor.java:84) at org.broadleafcommerce.common.web.dialect.AbstractModelVariableModifierProcessor.processElement(AbstractModelVariableModifierProcessor.java:46) at org .thymeleaf.processor.element.AbstractElementProcessor。