Expand my Community achievements bar.

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

Getting an exception in Model.

Avatar

Level 5

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

2 Replies

Avatar

Community Advisor

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

Try adding @Deleted Account to those specific fields

e.g 

@Inject @Deleted Account

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

Community Advisor

@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)