Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Resource Adapt To Custom Model Class Issue with Model Factory Get Image Class

Avatar

Level 2

We have two components:

Book Component (sling:resourceSuperType="xyz/components/image")

Book Listing Component

 

In Book Component Model Implementation, init method we are using Modal Factory to get image model.

 

"image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);"

 

In Listing component Component Model Implementation, init method based on some filters, we are getting few Book Pages. From these book pages we are getting their respective book details node which we are trying to adapt to Book Model.

 
"BookDetails bookDetails = bookDetailsResource.adaptTo(BookDetails.class);"
 
But we are getting following error:
"org.apache.sling.models.impl.ModelAdapterFactory Could not adapt to model"
"org.apache.sling.models.factory.PostConstructException: Post-construct method has thrown an exception for model class com.xyz.core.models.impl.BookDetailsImpl"
"Caused by: java.lang.NullPointerException: null
at com.xyz.core.models.impl.BookDetailsImpl.initModel"
 
Since we are doing adapt to from resource object, request will be null; thats why we even tried to do as below:
 
"image = modelFactory.createModel(resource, Image.class);"
 
But both of these did not work.
 
We need suggestions from the team.
 
 
1 Accepted Solution

Avatar

Correct answer by
Community Advisor
7 Replies

Avatar

Community Advisor

Hello @S__k__Agarwal ,

BookDetails bookDetails = bookDetailsResource.adaptTo(BookDetails.class);

 The following code should work. But before that, I will recommend you to make sure the bookDetailsResource has the property,

sling:resourceType=xyz/components/book

Also, the BookDetails model class is adaptable with Resource.

@Model(adaptables = Resource.class,

 

Avatar

Level 2

Hello @Sady_Rifat,

 

I have done exactly the same thing but the issue is with book image.

 

In Book Component Model Implementation, init method we are using Modal Factory to get image model.

 

"image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);"

 

adaptTo is giving the above error in this line.

 

Thanks & Regards,

Sumit

Avatar

Community Advisor

Thanks for responding,
image = modelFactory.createModel(resource, Image.class);

also, you mentioned image = resource.adaptTo(Image.class);

Both of them are getting null point exceptions.

 

I am assuming your Annotate modelFactory is not null. It is perfectly initialized.

Since both line doesn't work, it's pretty much sure the problem is in your resource.

Can you please make sure the resource has this?

sling:resourceType=xyz/components/image

And the Image model is adaptable. I also assumed the image is your custom image reference and you followed the Sling Deligation Pattern.

Avatar

Level 2

Hello @Sady_Rifat 

 

1. Yes, I even tried image = modelFactory.createModel(resource, Image.class);

 

2. No, I never mentioned this image = resource.adaptTo(Image.class);

 

3. Yes, my model factory is not null. ModelFactory org.apache.sling.models.impl.ModelAdapterFactory@28b8df3d

 

4. my resource is having resource type as below:

Book Component

(sling:resourceType="xyz/components/book")

(sling:resourceSuperType="xyz/components/image")

 

Avatar

Community Advisor

I tried to reproduce the issue on my local environment. In both the BookList and BookDetails sling model classes, I used the getModelFromWrappedRequest method from the ModelFactory class. It is functioning as expected. 

// BookDetails Model init [Book Component]
Image image = modelFactory.getModelFromWrappedRequest(
  request, resource, Image.class);

// BookList Model init [Book Listing Component]
BookDetails book = modelFactory.getModelFromWrappedRequest(
  request, resource, BookDetails.class);

Additionally, I have pushed the code to a GitHub repository, and you can review the pull request for your reference. https://github.com/MahediSabuj/aem-community/pull/1/files 

 

 

Avatar

Correct answer by
Community Advisor

Avatar

Administrator

@S__k__Agarwal Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni