Current Node is NULL in Components | Community
Skip to main content
Nishadh
Level 2
October 16, 2015
Solved

Current Node is NULL in Components

  • October 16, 2015
  • 7 replies
  • 3802 views

Hi,

Am newbie to AEM. Am creating pages with component included like <cq:include path="" resourceType="/customComponent"/>. And i created dialog for getting text field and path field value. 

And now am trying to access my current node in customComponent.jsp using both way  PropertyIterator it = currentNode.getProperties() or resourceResolver.adaptTo(Node.class) but the currentNode throws Null Pointer Exception . Now i need to get all Properties from multi field and path field components. How can i achieve this. Please give me some solution and thanks in advance.

P.S: Am added global.jsp too.

Am got stuck up with this issue.

Thanks,

Mohamedh Nishadh

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 Pawan-Gupta

Hello,

There are bunch of implicit objects available in your servlet (jsp) when you use global.jsp or explicitly call <cq:defineObjects/> and one of them is "properties" which is a direct map of properties from your current node so you need not require to do currentNode.getProperties() just use properties also refer http://docs.adobe.com/docs/en/cq/5-6-1/developing/components.html

now to access a single value property from node use -String val= properties.get(propertyName, String.class);  // will return null if value not available and if you want to prevent from null then pass an empty string instead String.class

to access multivalue property (like multifield) from node then use - String []val=(String[])properties.get(propertyName,String[].class);

 

Thanks,

Pawan

7 replies

narayanank84409
Level 4
October 16, 2015

Hi Nishadh,

Try to give entire path in resource type

Ex : 

<cq:include path="mynode" resourceType="myapp/components/somecomponent" />
smacdonald2008
Level 10
October 16, 2015

This looks like the pages are not properly setup - that is why you are getting nulls on nodes.  

As a new user - i highly recommend that you go through this content to learn how to setup an AEM site and pages:

http://docs.adobe.com/docs/en/aem/6-0/develop/the-basics/website.html

If the pages are setup properly - then it works.

Look at this community article that uses the SLign API and get page node properties:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

You can even pass nodes from front end component to back end OSGi services and the nodes are not null:

https://helpx.adobe.com/experience-manager/using/passing_nodes.html

narayanank84409
Level 4
October 16, 2015

Try to get the property like this

Title : <%=currentNode.getProperty("jcr:title").getString()%>

smacdonald2008
Level 10
October 16, 2015

As a new user - i recommend that you look at this artilce:

http://docs.adobe.com/docs/en/aem/6-0/develop/the-basics/website.html

Once you properly setup an AEM site and nodes - you will not get NULLs on nodes. 

See this artilce on using SLING API to read nodes:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

Pawan-Gupta
Pawan-GuptaAccepted solution
Level 8
October 16, 2015

Hello,

There are bunch of implicit objects available in your servlet (jsp) when you use global.jsp or explicitly call <cq:defineObjects/> and one of them is "properties" which is a direct map of properties from your current node so you need not require to do currentNode.getProperties() just use properties also refer http://docs.adobe.com/docs/en/cq/5-6-1/developing/components.html

now to access a single value property from node use -String val= properties.get(propertyName, String.class);  // will return null if value not available and if you want to prevent from null then pass an empty string instead String.class

to access multivalue property (like multifield) from node then use - String []val=(String[])properties.get(propertyName,String[].class);

 

Thanks,

Pawan

shaheenas113483
Level 3
March 13, 2019

I used String []val=(String[])properties.get(propertyName,String[].class) to retreive value, how do I add value to a multifield?

arunpatidar
Community Advisor
Community Advisor
March 13, 2019

You have to create an array of values:

ValueFactory valueFactory = session.getValueFactory(); 
Node node = session.getNode("/content/path/to/my/node");
Value[] values = new Value[3];
values[0] = valueFactory.createValue("First value");
values[1]  = valueFactory.createValue("Second value");
values[2] = valueFactory.createValue("Third value");

node.setProperty("propertyName", values);

alternatively, you may use a String array:

node.setProperty("propertyName", new String[] {"First value", "Second value", "Third value"}); 

Node :

since node is global , you need to adapt from resource object or from SUBSERVICE session.

Arun Patidar