title: Java: HashMap works, LinkedHashMap gets Null Pointer Exception
body: <p>I started learning Java a few weeks ago, coming from PHP and no other prior language background. I'm used to just using array()...</p>
  
  <p>When I changed my class from a HashMap to a LinkedHashMap I'm getting a Null Pointer Exception, </p>
  
  <p>Using JDK 1.6</p>
  
  <p><em><strong>Class LinkedHashMap</strong> - From Oracle <a href="http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html" rel="nofollow">Oracle JDK 1.6 Reference</a></em></p>
  
  <blockquote>
    <p>This class provides all of the optional Map operations, and permits null elements. Like HashMap, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. Performance is likely to be just slightly below that of HashMap, due to the added expense of maintaining the linked list, with one exception: Iteration over the collection-views of a LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.</p>
  </blockquote>
  
  <p><strong>Does Not Work</strong></p>
  
  <pre><code>@Component("blMenu")
  public class MenuProcessor extends AbstractModelVariableModifierProcessor {
  public  List&lt;Category&gt; topLevelCategories;
  public  LinkedHashMap&lt;Category, LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt;&gt; navigationMenu = new LinkedHashMap&lt;Category, LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt;&gt;();
  public List&lt;Category&gt; 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&lt;Category&gt; categories = catalogService.findCategoriesByName(parentCategory);
      if (categories != null &amp;&amp; categories.size() &gt; 0) {
          // gets child categories in order ONLY if they are in the xref table and active
          List&lt;Category&gt; subcategories = categories.get(0).getChildCategories();
          if (subcategories != null &amp;&amp; !subcategories.isEmpty()) {
              if (StringUtils.isNotEmpty(unparsedMaxResults)) {
                  int maxResults = Integer.parseInt(unparsedMaxResults);
                  if (subcategories.size() &gt; maxResults) {
                      subcategories = subcategories.subList(0, maxResults);
                  }
              }
          }
          topLevelCategories = subcategories;
          buildMenu(arguments, catalogService);
      }
  }
  
  public void buildMenu(Arguments arguments, CatalogService catalogService)
  {
      LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt; chunkSubs;
  
      for(Category topLvlCat : topLevelCategories)
      {
  
          Categories = catalogService.findCategoriesByName(topLvlCat.getName());
          List&lt;Category&gt; subCategories = Categories.get(0).getChildCategories();
  
          if(subCategories != null &amp;&amp; !subCategories.isEmpty())
          {
              subCategoriesSize = subCategories.size();
              chunkSubs = chunkSubCategories(subCategories);
              navigationMenu.put(topLvlCat, chunkSubs);
          }
          else
          {
              navigationMenu.put(topLvlCat, null);
          }
      }
      addToModel(arguments, resultVar, navigationMenu);
  }
  
  
  public LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt; chunkSubCategories(List&lt;Category&gt; subCategories)
  {
      List&lt;Category&gt; chunkedCategories;
      LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt; subCategoriesChunked = new LinkedHashMap&lt;Integer, List&lt;Category&gt;&gt;();
  
      int iLoop = 0;
      int start = 0;
      int end = chunkSize - 1;
      int catSize = subCategoriesSize-1;
  
      if(subCategoriesSize &gt; 1){
      do
      {
  
          if(end &lt; 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 &gt; iLoop);
      }
      return subCategoriesChunked;
  }
  
  
  }
  </code></pre>
  
  <p>But change all the LinkHashMaps to HashMaps and it works.</p>
  
  <p>Stack Trace</p>
  
  <blockquote>
    <p>Mar 20, 2013 12:37:46 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [mycompany] in context with path [/site] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'com.mycompany.processor.MenuProcessor' (layout/partials/nav:8)] with root cause
    java.lang.NullPointerException
            at com.mycompany.processor.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.doProcess(AbstractElementProcessor.java:74)
        at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
        at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:896)
        at org.thymeleaf.dom.Node.processNode(Node.java:858)
        at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:639)
        at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:621)
        at org.thymeleaf.dom.Node.processNode(Node.java:876)
        at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:639)
        at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:621)
        at org.thymeleaf.dom.Node.processNode(Node.java:876)
        at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:639).....</p>
  </blockquote>