title: Java getParent returning null
body: <p>I'm really new to Java so bear with me here. I have a class that essential gets the parent of a page and returns it. When I navigate to the very top of a node I get a null pointer exception which is the line that reads <strong>for(Page page = to........</strong> I understand why because at the top node there is no parent.  How do I prevent my code from generating an error and gracefully displays a message if a user does navigate to the top level node.</p>
  
  <p>Class Code:</p>
  
  <pre><code>public class Pages {
  public static List&lt;Page&gt; getPath(Page from, Page to) {
      if (from == null || to == null) throw new IllegalArgumentException();
  
      List&lt;Page&gt; path = new ArrayList&lt;Page&gt;();
      for (Page page = to.getParent(), last = from.getParent(); page != null &amp;&amp; !(page.getPath().equals(last.getPath())); page = page.getParent())
          path.add(page);
      Collections.reverse(path);
      return path.contains(from) ? path : null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>JSP Code:</p>
  
  <pre><code>Page rootPage = resourceResolver.adaptTo(PageManager.class).getPage(properties.get("rootNode",Page)currentPage).getPath()));
  List&lt;Page&gt; listPages = Pages.getPath(rootPage, currentPage);
  
  for (Page showContent : listPages) {
  %&gt;
  &lt;li&gt;&lt;a href="#"&gt;listPages.getDisplayTitle(showContent)) %&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;%
  } //end page for loop
  </code></pre>