Expand my Community achievements bar.

SOLVED

Getting error log "AEM failed to activate Use class"

Avatar

Level 3

Hey Community

I have wriiten this basic code to get cq:tags property of current page

public Void activate throws exception{

rootpage=getCurrentPage;

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content/");

         ValueMap pageProperties = res .getValueMap();

         tag= (String) pageProperties.get("cq:tags", "Default value");

logger.info(tag);

}

 

When I'm using creating object using HTL statement in HTMl file it is not displaying anything on page and error log says "AEM failed to activate Use class"

How to resolve it?

@Ritesh_Mittal 

@Dipti_Chauhan Please suggest.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ShagunMalik 

BTW there is no need to add slash after jcr:content. It should be 

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content");

View solution in original post

18 Replies

Avatar

Community Advisor

Hi @ShagunMalik ,

 

You must be getting some exception like NullPoineterException or something in the logs which is happening inside Active method of your class. Could you provide the error logs?

Avatar

Community Advisor

Hi @Sh 

 May be there is some exception in activate method. Please debug your code using debugger and find out where you getting exception.

 

And normally you should not write any logic inside activate method. Create different getter to get properties because if there is any issue in you code written within activate method your class activation will get failed

 

Thanks

Dipti

 

Avatar

Correct answer by
Community Advisor

Hi @ShagunMalik 

BTW there is no need to add slash after jcr:content. It should be 

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content");

Avatar

Level 3
@Dipti_Chauhan, thank you for that but the issue is still the same. I'm getting error as above and it is throwing NullPoineterException: null.

Avatar

Community Advisor
you need to debug this. At what point you getting NLP. could be your resource null or resourceresolver object is null.

Avatar

Level 3
@Dipti_Chauhan can you tell me how to convert rootpage,res and pageProperties to string so I can use Logger.info() command to see which object is null?

Avatar

Community Advisor
@ShagunMalik By debugging I mean run your AEM in dubug mode and starting adding breakpoints in your class.More information you will find here

Avatar

Community Advisor
@ShagunMalik And can you also share your complete Java class?

Avatar

Level 3

@Dipti_Chauhan, Here is my complete java code:

 

Public class CategoryUse extends WCMUsePojo{

private static final Logger logger =LoggerFactory.getlogger(CategoryUse.class)

private String tag;

private Page rootpage;

ValueMap pageProperties;

 

public String getTag{

return tag;

}

 

public void activate() throws exception{

rootpage=getCurrentPage;

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content");

         ValueMap pageProperties = res .getValueMap();

         tag= (String) pageProperties.get("cq:tags");

logger.info(tag);

}

}

}

Avatar

Level 3
One more doubt @Dipti_Chauhan, cq:tags is of type String[] in page property so that can be the issue as I'm storing it in String and not String[]?

Avatar

Community Advisor
HI @ShagunMalik yes this will be multivalue and need to be stored in String[].And as I was mentioning you already have page object than you can directly use page object to get properties. e.g : final String[] cqTags = page.getProperties().get("cq:tags", String[].class);

Avatar

Employee Advisor

@ShagunMalik ,

 

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content/");

remove the slash from the end

Resource res = getResourceResolver.getResource(rootpage.getPath()+"/jcr:content");

 

if still the issue persists add loggers or run your class in debug mode to get the exact line where there is exception

 

Hope this helps

Avatar

Level 2

Hi @ShagunMalik ,

 

Could you please verify below line

tag= (String) pageProperties.get("cq:tags", "Default value");

 

By Default  cq:tags are String Array not a String Field 

String[] tagArrays =  pageProperties.get("cq:tags", String[].class);

if(Objects.nonNull(tagArrays ) && tagArrays .length>0) {

     tag = tagArrays[0]; -- will give first value in array.

}

Avatar

Level 3

Hey, @GnanendraPonnala  I get your point but Even if I'm trying to access any other property of page which is of type String such as cq:template then still it is not working.