Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

groovy exception

Avatar

Level 3

I'm trying check if a container is present or not in groovy script but it's throwing null pointer exception I'm handling that as below,

getPage(parent).recurse {
        page ->
            def content = page.node
        def property = content.get('sling:resourceType')
        if (property == "project/components/product-page") {
            count++;
            println page.path;
         def footercontainer =  getNode(page.path+"/jcr:content/root/container_1845659030");
         def refcontainer = getNode(page.path+refNode);
         if(getNode(page.path+footercontainer) != null){
                if(footercontainer.hasProperty(propName1)){
                (footercontainer).getProperty(propName1).remove();
             }
         }

7 Replies

Avatar

Community Advisor

Hi @KKeerthi 
You need to use node methods to check properties in the jcr node

 def property = content.getProperty('sling:resourceType').getString();



Arun Patidar

Avatar

Level 3

able to get pages with template but for some pages that container is not present, so there it's throwing nullpointer exception which I'm not able to handle through groovy.

Avatar

Level 3

in which line were you getting null pointer exception 

Avatar

Community Advisor

Hi @KKeerthi 
The reason you are getting a null pointer exception is because you are passing the footercontainer variable to the getNode method, which is expecting a string path. Instead, you should pass the string path directly to the getNode method.

getPage(parent).recurse { page ->
def content = page.node
def property = content.get('sling:resourceType')
if (property == "project/components/product-page") {
count++
println page.path
def footercontainer = getNode(page.path+"/jcr:content/root/container_1845659030")
def refcontainer = getNode(page.path+refNode)
if (getNode(page.path+"/jcr:content/root/container_1845659030") != null) {
if (footercontainer.hasProperty(propName1)) {
footercontainer.getProperty(propName1).remove()
}
}
}
}

we pass the string path directly to the getNode method instead of passing the footercontainer variable. This should resolve the null pointer exception. 





Avatar

Administrator

@KKeerthi can you please share how you fixed this?

 



Kautuk Sahni

Avatar

Administrator

@KKeerthi Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni