Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to combine two ValueMaps?

Avatar

Level 9

trying to combine 2 valuemaps into 1.   putAll fails with null pointer, and it doesnt not seem possible to create an empty valueMap.  Any ideas for another option? I have tried posting the full info on why we are doing this, but the forum system always deletes it, so trying this short version.

 

		predicate.put("path", path);
		predicate.put("type", "cq:unstructured");
		predicate.put("group.1_property", "sling:resourceType");
		predicate.put("group.1_property.value", "mysite/components/gamemain");
		predicate.put("group.2_property", "sling:resourceType");
		predicate.put("group.2_property.value", "mysite/components/gameterms");
		predicate.put("group.p.or", "true");
		
		Query query = _builder.createQuery(PredicateGroup.create(predicate), session);
		
		// should only be 2 components!
		query.setStart(0);
		query.setHitsPerPage(20);
		
		SearchResult searchResult = query.getResult();
		
		ValueMap allProps = null;
		
		for(Hit hit : searchResult.getHits()) {
			ValueMap props = hit.getProperties();
			if (allProps == null) {
				allProps  = props;
			}  else {
				allProps.putAll(props);  // null pointer exception
			}
		}

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @TB3dock,

Ways to instantiate an empty ValueMap is through its implementing Decorator classes. 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);

Example :

 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);
/* Within Query results iteration */
for (Hit hit : queryResults.getHits()) {
			try {
				String pagePath = hit.getPath();								
				vm.putAll(hit.getProperties());
....

 

Below API doc has the list of all known implementing classes of ValueMap, you can choose the one based on your need. (Ignore Deprecated ones from the list)

https://sling.apache.org/apidocs/sling8/org/apache/sling/api/resource/ValueMap.html

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @TB3dock,

Ways to instantiate an empty ValueMap is through its implementing Decorator classes. 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);

Example :

 

Map<String, Object> map = new HashMap<String, Object>();
ValueMap vm = new ValueMapDecorator(map);
/* Within Query results iteration */
for (Hit hit : queryResults.getHits()) {
			try {
				String pagePath = hit.getPath();								
				vm.putAll(hit.getProperties());
....

 

Below API doc has the list of all known implementing classes of ValueMap, you can choose the one based on your need. (Ignore Deprecated ones from the list)

https://sling.apache.org/apidocs/sling8/org/apache/sling/api/resource/ValueMap.html

Avatar

Community Advisor

@TB3dock 

You will get NullPointerException if any one of variables is null. 

allProps.putAll(props);

allProps and props should not be null. Please check.