Expand my Community achievements bar.

SOLVED

AEM 6.3 Sling Models Error: Cannot be correctly instantiated by the Use API.

Avatar

Level 3

I am working on a AEM 6.1 to 6.3 Migration project. After replacing all the deprecated methods/api's like admin resource resolver and WCMUse with getServiceResourceResolver and WCMUsePojo respectively, my application is throwing an error on sling models (as below). Basically it's not getting instantiated -

Error Says:

1446899_pastedImage_0.png

Code is written like:

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.List;

import java.util.Locale;

import java.util.Map;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import org.apache.commons.lang3.BooleanUtils;

import org.apache.commons.lang3.StringUtils;

import org.apache.felix.scr.annotations.Reference;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.commons.json.JSONException;

import org.apache.sling.commons.json.JSONObject;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.Self;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;

import com.google.gson.Gson;

/**

* Sling Model for getting selectValue.

*/

@Model(adaptables = SlingHttpServletRequest.class)

public class SelectValue {

    private static final Logger LOG = LoggerFactory.getLogger(SelectValue.class);

    @Inject

    private Page currentPage;

    @Inject @Self

    private SlingHttpServletRequest request;

    private String localCode;

  

    @PostConstruct

    public void init() {     

        localCode = LanguagesUtil.getLanguageLocaleCode(currentPage.getPath());

..........

Is there any issue with the code written?

Has anyone encountered this type of issue while migrating aem 6.1 to 6.3?

1 Accepted Solution

Avatar

Correct answer by
Level 3

Thankyou All, This issue has been resolved.

It was related to the node property (jcr:title), which was coming as null as it was not present in the CRX/DE for that particular node, So, it was throwing an error.

View solution in original post

7 Replies

Avatar

Administrator

Check the :

Re: org.apache.sling.scripting.sightly.SightlyException: Identifier XXXX cannot be correctly instant...

Can you please test this article/package in your instance:- Creating a custom Touch UI Grid Component for Adobe Experience Manager

// This  AEM 6.3 Sling Model article that works and does not throw any exception.



Kautuk Sahni

Avatar

Level 4

Hi @kautuk_sahni ! 
Can I get the another URL for Creating a custom Touch UI Grid Component for Adobe Experience Manager ? I'm not able to access it. Its giving "404. Page not found."
I want to try that, so please provide me with other source if possible. Thanks

Avatar

Community Advisor

Check the answers Kautuk gave. In addition, if you can give the log trace, may be we can clearly identify what is the exact root cause.

Avatar

Level 10

Test this code on a fresh AEM install and see if it throws the same error. This will tell us if the issue is with the code or upgraded AEM version.

Avatar

Correct answer by
Level 3

Thankyou All, This issue has been resolved.

It was related to the node property (jcr:title), which was coming as null as it was not present in the CRX/DE for that particular node, So, it was throwing an error.

Avatar

Level 7

Hi,

The solution for this error is to use @Optional annotation while trying to inject fields using Sling Models. The reason is as follows;

When you are using Sling Models for injecting fields and you try to drag and drop the component for the first time, the model will check for the all the injected values. Since you are dropping the component for the first time and there are no values stored for it in the JCR and as result the error will be thrown. But when we use the @Optional annotation the injection strategy will be considered as optional for the field and hence it won't check for the values.

Please refer to following link for deeper knowledge on Sling Models:

Apache Sling :: Sling Models

Regards,

TechAspect Solutions