How to improve the code to get a child node ValueMap
I was getting a NullPointerException in my method in the following line of code
ValueMap heroTeaserImageValueMap = resource.getChild("image").getValueMap()so I rewrote it to:
ValueMap heroTeaserImageValueMap = getImageValueMap(heroTeaser);
private ValueMap getImageValueMap(Resource resource) {
if (resource.hasChildren()) {
return resource.getChild("image").getValueMap();
} else {
return ValueMap.EMPTY;
}
}
This is not giving me the NullPointerException since I make sure I check if node has children nodes and from the data structure in the repo I know the child node name is image. Can someone suggest how I can improve this code.
This is how the resource looks like in the repo
"heroteaser": {
"jcr:primaryType": "nt:unstructured",
"targetBlank": "false",
"bgColor": "blue",
"teaserLead": "Home Hero Teaser Lead",
"linkType": "button",
"sling:resourceType": "xxx/heroTeaser",
"teaserTitle": "Home Hero Teaser Title",
"image": {
"jcr:primaryType": "nt:unstructured",
"fileReference": "/content/dam/xxx/Alltag_23-600x750.jpeg"
}
},