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 for(Page page = to........ 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.
Class Code:
public class Pages {
public static List<Page> getPath(Page from, Page to) {
if (from == null || to == null) throw new IllegalArgumentException();
List<Page> path = new ArrayList<Page>();
for (Page page = to.getParent(), last = from.getParent(); page != null && !(page.getPath().equals(last.getPath())); page = page.getParent())
path.add(page);
Collections.reverse(path);
return path.contains(from) ? path : null;
}
}
JSP Code:
Page rootPage = resourceResolver.adaptTo(PageManager.class).getPage(properties.get("rootNode",Page)currentPage).getPath()));
List<Page> listPages = Pages.getPath(rootPage, currentPage);
for (Page showContent : listPages) {
%>
<li><a href="#">listPages.getDisplayTitle(showContent)) %></a></li>
<%
} //end page for loop