有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

currentNode上的jcr Java空检查

我对爪哇很陌生,所以请容忍我。我有一小段代码 检查currentNode是否具有“fileReference”属性并返回其值。 但我的空检查似乎不起作用,因为如果fileReference中没有任何内容 我犯了个错误。如果存在对fileReference的引用,则可以正常工作。以下是错误:

Caused by: javax.jcr.PathNotFoundException: fileReference

这是我的代码:

    if(currentNode != null){
        NodeIterator checkNode = currentNode.getNodes();

        while (checkNode.hasNext()) {
            Node imageNode = checkNode.nextNode();
            printNodeTitle = imageNode.getProperty("fileReference").getString();
        }
    } 

       public String getImageNode() { (printNodeTitle != null) ? return printNodeTitle : return ""; }

非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    我不是{}方面的专家,但试试这个

    if(currentNode != null){
        NodeIterator checkNode = currentNode.getNodes();
    
        while (checkNode.hasNext()) {
            Node imageNode = checkNode.nextNode();
            Iterator<Node> fileReferences = imageNode.getProperties("fileReference");
            if(fileReferences.hasNext()) { // You might want to improve this
                printNodeTitle = fileReference.next().getString(); // You might want to improve it
            }
        }
    } 
    

    如果找不到任何节点并且检索到空的Iterator,则使用getProperties(String)检索所有节点

    Node fileReference...我刚刚猜到了类型(Node),你必须改变它