Resource Adapt To Custom Model Class Issue with Model Factory Get Image Class | Community
Skip to main content
Level 3
October 16, 2023
Solved

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

  • October 16, 2023
  • 4 replies
  • 2575 views

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.
 
 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

Sady_Rifat
Community Advisor
Community Advisor
October 16, 2023

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,

 

Level 3
October 16, 2023

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

Sady_Rifat
Community Advisor
Community Advisor
October 16, 2023

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.

Mahedi_Sabuj
Community Advisor
Community Advisor
October 16, 2023

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 

 

 

Mahedi Sabuj
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
October 18, 2023
kautuk_sahni
Community Manager
Community Manager
October 19, 2023

@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