groovy exception | Community
Skip to main content
February 4, 2024

groovy exception

  • February 4, 2024
  • 4 replies
  • 1808 views

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();
             }
         }

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

arunpatidar
Community Advisor
Community Advisor
February 4, 2024

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

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

Arun Patidar
KKeerthiAuthor
February 5, 2024

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.

ashwinka
February 17, 2024

in which line were you getting null pointer exception 

Raja_Reddy
Community Advisor
Community Advisor
February 5, 2024

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. 



KKeerthiAuthor
February 5, 2024

Fixed this.

kautuk_sahni
Community Manager
Community Manager
February 8, 2024

@kkeerthi can you please share how you fixed this?

 

Kautuk Sahni
kautuk_sahni
Community Manager
Community Manager
February 6, 2024

@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