currentNode object throws nullpointer exception | Community
Skip to main content
Riju-Mahna
Level 2
October 16, 2015
Solved

currentNode object throws nullpointer exception

  • October 16, 2015
  • 6 replies
  • 4287 views

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 ?

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

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

6 replies

Adobe Employee
October 16, 2015

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

Adobe Employee
October 16, 2015

Hi Riju,

Can you provide the stacktrace?

Riju-Mahna
Level 2
October 16, 2015

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 ?

Riju-Mahna
Level 2
October 16, 2015

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

JustinEd3Adobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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

Ojjis
Level 7
October 16, 2015

Could you use this for the session instead ?

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



/Johan