Java getParent 返回 null [英] Java getParent returning null

查看:77
本文介绍了Java getParent 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 真的很陌生,所以请耐心等待.我有一个类,它必须获取页面的父级并返回它.当我导航到节点的最顶部时,我得到一个空指针异常,该异常是读取 for(Page page = to........ 我明白为什么,因为在顶部节点没有父节点.如果用户确实导航到顶级节点,我如何防止我的代码生成错误并优雅地显示消息.

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.

班级代码:

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 代码:

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

推荐答案

既然你要求了一个例子:

Since you requested an example:

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>();
    Page page=to.getParent();
    Page last=from.getParent();
    // I'm assuming getPath() can be null occassionaly and is a String
    String lastPath;
    if(last!=null && (lastPath=last.getPath())!=null){
    // The assignment above is an acceptable one, as it saves a nested if statement
        // traverse your path
        while(page!=null && page.getPath()!=null && !(page.getPath().equals(lastPath))) {
            path.add(page);
            page=page.getParent();
        }
    }
    Collections.reverse(path);
    return path.contains(from) ? path : null;
}

值得注意的是,如果您为from"或to"参数输入null",这段代码仍然会破坏您的页面,因为在这种情况下您将抛出一个新的 IllegalArgumentException.我更喜欢在实际的 java 后端处理尽可能多的逻辑和错误处理,而在 jsp 前端处理尽可能少的逻辑和错误处理.如果存在无效参数,可能的改进可能是只返回一个空列表.

It is worth noting that this piece of code can still break your page if you enter "null" for your "from" or "to" argument since you are throwing a new IllegalArgumentException in that case. I prefer to handle as much of the logic and error handling in the actual java backend and as little as possible on the jsp frontend. A possible improvement could be to have return just an empty list if there are invalid params.

public class Pages {
public static List<Page> getPath(Page from, Page to) {
if (from == null || to == null) return new ArrayList<Page>();

List<Page> path = new ArrayList<Page>();
Page page=to.getParent();
Page last=from.getParent();
// I'm assuming getPath() can be null occassionaly and is a String
String lastPath;
if(last!=null && (lastPath=last.getPath())!=null){
// The assignment above is an acceptable one, as it saves a nested if statement
    // traverse your path
    while(page!=null && page.getPath()!=null && !(page.getPath().equals(lastPath))){
        path.add(page);
        page=page.getParent();
    }
}
Collections.reverse(path);
return path;//return path or empty list
}

通过这项改进,您不应再收到任何异常.在前端,您现在可以检查您的列表是否为空并采取相应措施

With this improvement, you shouldn't get any execeptions anymore. In the frontend you can now just check if your list is empty or not and act accordingly

*旁注 1:* 我不确定在什么情况下您想将从"添加到路径中(所以我离开了它out),因此您可能需要将其添加到提供的代码中.

*Side note 1:* I'm not really sure under what circumstances you'd want to add the "from" to the path (so I left it out), so this is something you might need to add to the provided code.

*旁注 2:*你可能想考虑 EL 而不是 scriptlet,只是一个想法:)

*Side note 2:* You might want to consider EL instead of scriptlets, just a thought :)

这篇关于Java getParent 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆