Expand my Community achievements bar.

SOLVED

currentNode object throws nullpointer exception

Avatar

Level 3

In my component's JSP, if I want to reference the current page node using "currentNode" object, it throws a NullPointerException and the whole component then throws a ScriptEvaluationException

My component JSP does have the 'global.jsp' included at the top.

Also, the component throws these exceptions only in a particular site. It works fine in others.

What possible could be the reason for this ? In what conditions does the "currentNode" object be NULL ?

1 Accepted Solution

Avatar

Correct answer by
Employee

Riju Mahana wrote...

I didn't understand the bit "isn't adaptable to a JCR Node object". AFAIK, when we add a component using <cq:include> , it is added to the JCR below the page.

Nope. If you pass a path to <cq:include> which doesn't exist, nothing is added to the JCR (this would represent a security issue). Rather a synthetic resource is created. Since it isn't a JCR Node, it can't be adapted to the node.

My main concern is to get a Query Object for my search component. I need to use

 
  1. currentNode.getSession().getWorkspace().getQueryManager().createQuery()

in my component. With CurrentNode being null,. Is there any other way to get it done ?

 

resourceResolver.adaptTo(Session.class).getWorkspace().getQueryManager().createQuery() would be the correct way to do this.

Better yet would be to stay entirely in the Sling API:

Iterator<Resource> queryResults = resourceResolver.findResources(stmt, language);

View solution in original post

6 Replies

Avatar

Employee

currentNode could be null if the current resource isn't adaptable to a JCR Node object, i.e. if it was from a different Resource Provider

Avatar

Employee

Hi Riju,

Can you provide the stacktrace?

Avatar

Level 3

I didn't understand the bit "isn't adaptable to a JCR Node object". AFAIK, when we add a component using <cq:include> , it is added to the JCR below the page.

My main concern is to get a Query Object for my search component. I need to use

currentNode.getSession().getWorkspace().getQueryManager().createQuery()

in my component. With CurrentNode being null,. Is there any other way to get it done ?

Avatar

Level 3

I've attached the stacktrace. The gist is below:

Line 72 of my component is:

Query query = currentNode.getSession().getWorkspace().getQueryManager().createQuery(stmt, Query.SQL);

--------------------------------

com.day.cq.wcm.core.impl.WCMDebugFilter Error during include of SlingRequestPathInfo: path='/content/mysite/en/insights/jcr:content/mysearch', selectorString='null', extension='html', suffix='null' org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/mysite/components/insights/LandingSearch/LandingSearch.jsp at line 72
  ....
Caused by: org.apache.sling.api.SlingException: An exception occurred processing JSP page /apps/mysite/components/insights/LandingSearch/LandingSearch.jsp at line 72
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:683)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:608)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:533)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:241)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:86)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:441)
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:361)
    ... 157 more
Caused by: java.lang.NullPointerException
    at org.apache.jsp.apps.mysite.components.insights.LandingSearch.LandingSearch_jsp._jspService(LandingSearch_jsp.java:223)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:502)
    ... 162 more

Avatar

Correct answer by
Employee

Riju Mahana wrote...

I didn't understand the bit "isn't adaptable to a JCR Node object". AFAIK, when we add a component using <cq:include> , it is added to the JCR below the page.

Nope. If you pass a path to <cq:include> which doesn't exist, nothing is added to the JCR (this would represent a security issue). Rather a synthetic resource is created. Since it isn't a JCR Node, it can't be adapted to the node.

My main concern is to get a Query Object for my search component. I need to use

 
  1. currentNode.getSession().getWorkspace().getQueryManager().createQuery()

in my component. With CurrentNode being null,. Is there any other way to get it done ?

 

resourceResolver.adaptTo(Session.class).getWorkspace().getQueryManager().createQuery() would be the correct way to do this.

Better yet would be to stay entirely in the Sling API:

Iterator<Resource> queryResults = resourceResolver.findResources(stmt, language);

Avatar

Level 7

Could you use this for the session instead ?

Session session = resource.getResourceResolver().adaptTo(Session.class);



/Johan