Getting an exception in Model.

Avatar

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile
v1101
Level 2

30-07-2020

Hello All - I am getting an exception in the Model class.Can someone advise on this?

 

@Model(adaptables=Resource.class)
public class ModelA{
@ValueMapValue
@Named("cq:lastReplicated")
@Required
private String lastReplicated;


@Inject
@Named("articleTitle")
@SuppressWarnings("unchecked")
private String artTitle;

 

 

07.30.2020 21:57:32.160 *WARN* [ anonymous-nosite-0:0:0:0:0:0:0:1-24170aab-c015-47e6-9c11-512d9d960aab ] org.apache.sling.models.impl.ModelAdapterFactory Could not adapt to model
org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.xxx.yyy.aem.core.models.ModelA

org.apache.sling.models.impl.ModelAdapterFactory Could not adapt to model
org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.xxx.yyy.aem.core.models.ModelA

Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!

Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private java.lang.String com.xxx.yyy.aem.core.models.ModelA.artTitle
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:749)
... 163 common frames omitted
Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:635)
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:744)
... 163 common frames omitted
Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private java.lang.String com.xxx.yyy.aem.core.models.ModelA.altText
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:749)
... 163 common frames omitted

Replies

Avatar

Avatar
Validate 1
MVP
Albin_Issac
MVP

Likes

106 likes

Total Posts

119 posts

Correct reply

33 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Validate 1
MVP
Albin_Issac
MVP

Likes

106 likes

Total Posts

119 posts

Correct reply

33 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Albin_Issac
MVP

30-07-2020

Looks to be the some fields are missing in the resource while adapting into a model.

Try adding @Optional to those specific fields

e.g 

@Inject @Optional

private String otherName;

If the majority of the fields are optional then specify at the class level

e.g

@Model(adaptables=Resource.class, defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL)

public class MyModel {

Regards

Albin I

www.albinsblog.com

Avatar

Avatar
Establish
Level 2
fanindras
Level 2

Likes

15 likes

Total Posts

19 posts

Correct reply

1 solution
Top badges earned
Establish
Give Back
Boost 5
Boost 3
Boost 10
View profile

Avatar
Establish
Level 2
fanindras
Level 2

Likes

15 likes

Total Posts

19 posts

Correct reply

1 solution
Top badges earned
Establish
Give Back
Boost 5
Boost 3
Boost 10
View profile
fanindras
Level 2

31-07-2020

@v1101 ,

 

org.apache.sling.models.factory.MissingElementException is thrown, when your Model is not able to inject the property. As per your exception trace, you have two properties artTitle and altText, which are not getting injected to the model.


One possible reason could be, that the resource you are trying to adapt doesn't have these properties. To resolve the issue, you can follow either of the below:

1. Mark the private variable as "@Optional"

@Inject
@Optional
@Named("articleTitle")
@SuppressWarnings("unchecked")
private String artTitle;

2. Change the class level Model annotation to include the default injection strategy as optional.

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)