0

I'm pretty new to Java so bear with me. I've got a small bit of code that checks to see if a currentNode has a property of "fileReference" and returns its value. Except it doesn't seem like my null check is working because if nothing is in fileReference I get an error. If a reference to fileReference exists it works fine. Here is the error:

Caused by: javax.jcr.PathNotFoundException: fileReference

Here is my code:

    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 ""; }

Any help is greatly appreciated!

ssedano
  • 8,322
  • 9
  • 60
  • 98
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
  • Where are you calling `getImageNode()`? I'm guessing that you're building a path that isn't valid by transmogrifying null into the empty string. –  May 10 '13 at 22:10
  • getImageNode is just a getter I'm calling not sure what you mean by transmogrifying null into the empty string. – Delmon Young May 11 '13 at 02:45

2 Answers2

2

I'm pretty confident fileReference is actually a property, not a seperate node (since you are calling properties). Since you know the name of the property you want to get, I suggest getting it directly with a small check if it exists.

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

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

I'm assuming you deal with possible repository exceptions elsewhere

3xil3
  • 1,546
  • 1
  • 12
  • 16
  • wow! thanks @3xil3 worked like a charm. Your correct fileReference is in fact a property. thanks for the help! – Delmon Young May 11 '13 at 17:08
  • This is correct indeed, what Delmon was missing is found in the JCR javadocs at http://www.day.com/maven/jsr170/javadocs/jcr-2.0/javax/jcr/Node.html#getProperty(java.lang.String) : getProperty throws a PathNotFoundException if no property exists at the specified path or if the current Session does not have read access to the specified property. So you need to check beforehand that the property exists. – Bertrand Delacretaz May 15 '13 at 09:46
1

I'm not an expert on sling but try this

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
        }
    }
} 

You retrieve all nodes with getProperties(String), if no nodes are found and empty Iterator is retrieved.

The line Node fileReference... I just guessed the type (Node) you must change it.

ssedano
  • 8,322
  • 9
  • 60
  • 98
  • Thanks for replying @ssedano your right fileReference is a node. But for some reason I'm still getting the same error it doesn't make sense to me why this wouldn't work. – Delmon Young May 11 '13 at 02:44