I have a Service class as below which I deployed using Maven and is Active on Sling Web Console. When I am accessing the method getSearchAssetNames()
from this bundle it is getting called as per the AEM 6.0 logs on Authoring instance.
But, implicit objects such as repository, resource resolver factory, query builder are all getting null pointer exceptions through how I have derived them using @Reference
annotation.
Here is the code for the class. I have tried removing activate, deactivate methods, adding start/stop methods, everything, but still this does not work.
Error logs show:
*01.07.2015 12:05:24.014 INFO [127.0.0.1 [1435732523998] GET /content/test/en/headerfooter/jcr:content/footerpar/testassetfinder..html HTTP/1.1] com.test.example.assetfinder.AssetFinderImpl Query Builder: null 01.07.2015 12:05:24.014 INFO [127.0.0.1 [1435732523998] GET /content/test/en/fordheaderfooter/jcr:content/footerpar/testassetfinder..html HTTP/1.1] com.test.example.assetfinder.AssetFinderImpl JCR Repository: null Caused by: java.lang.NullPointerException: null at com.test.example.assetfinder.AssetFinderImpl.getSearchAssetNames(AssetFinderImpl.java:61)*
Can anyone please help me regarding how to solve this ?
package com.test.example.assetfinder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
/**
* Example Asset Finder in AEM DAM.
*/
@Service(value=com.test.example.assetfinder.AssetFinderService.class)
@Component
public class AssetFinderImpl implements AssetFinderService {
@Reference
private QueryBuilder builder;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private SlingRepository repository;
private static final Logger LOGGER = LoggerFactory.getLogger(AssetFinderImpl.class);
@Activate
protected void activate(final ComponentContext pCtx) throws RepositoryException {
}
@Deactivate
protected void deactivate(ComponentContext pCtx) throws RepositoryException {
}
public List<String> getSearchAssetNames() {
List<String> assetList = new ArrayList<String>();
Session session = null;
try {
LOGGER.info("Query Builder: " +builder);
LOGGER.info("Resolver Factory: " +resolverFactory);
LOGGER.info("JCR Repository: " +repository);
session = repository.loginAdministrative(null);
Map<String, String> map = new HashMap<String, String>();
map.put("path", "/content/dam");
map.put("type", "dam:Asset");
map.put("nodename", "*example*.*");
map.put("orderby.sort", "asc");
Query query = builder.createQuery(PredicateGroup.create(map), session);
SearchResult result = query.getResult();
// Iterating over the results
for (Hit hit : result.getHits()) {
assetList.add(hit.getTitle());
}
} catch(RepositoryException re) {
re.printStackTrace();
} finally {
if(null != session) {
session.logout();
}
}
return assetList;
}
}