Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Getting error log "AEM failed to activate Use class"

ShagunMalik
Level 3
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_M 

@Dipti_Chauhan Please suggest.

1 Accepted Solution
Dipti_Chauhan
Correct answer by
Community Advisor
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
Ritesh_M
Community Advisor
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?

Dipti_Chauhan
Community Advisor
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

 

Dipti_Chauhan
Correct answer by
Community Advisor
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

ShagunMalik
Level 3
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.
Dipti_Chauhan
Community Advisor
Community Advisor
you need to debug this. At what point you getting NLP. could be your resource null or resourceresolver object is null.
ShagunMalik
Level 3
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?
Dipti_Chauhan
Community Advisor
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
ShagunMalik
Level 3
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);

}

}

}

ShagunMalik
Level 3
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[]?
Dipti_Chauhan
Community Advisor
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);
Bimmi_Soi
Community Advisor
Community 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

GnanendraPonnala
Level 2
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.

}

ShagunMalik
Level 3
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.