Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Error while compiling a serivce

kunald48906615
Level 1
Level 1

Hi all,

I am getting the error while creating the service in AEM, as I have created the interface and when I am calling it in my model class it's throwing the error.

package com.aem.demo.core.models;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

import com.aem.demo.core.KeyService;

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

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

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

import org.apache.sling.settings.SlingSettingsService;

@Model(adaptables=Resource.class)

public class HelloWorldModel {

@Inject

private KeyService keyService ;

    @Inject

    private SlingSettingsService settings;

    @Inject @Named("sling:resourceType") @Default(values="No resourceType")

    protected String resourceType;

    private String message;

    @PostConstruct

    protected void init() {

  

   keyService.setKey(80);

  

        message = "\tHello World!-value of key service is"+keyService.getKey()+"\n";

        message += "\tThis is instance: " + settings.getSlingId() + "\n";

        message += "\tResource type is: " + resourceType + "\n";

    }

    public String getMessage() {

        return message;

    }

}

After compiling the above code

Errors:

[ERROR]   TestHelloWorldModel.setup:49 » NullPointer

1 Accepted Solution
Jörg_Hoh
Correct answer by
Employee
Employee

Hi,

how does your testclass look like? Remember, that if you just call the constructor of your model class and then call methods on it, the references and injections are not resolved.

Please see [1], [2] and [3] for references how to create unittests with sling models and SlingContext/AemContext.

HTH,

Jörg

[1] Writing unit tests for AEM — using SlingMocks – Things on a content management system

[2] Writing unit tests for AEM (part 2): Maven Setup – Things on a content management system

[3] Writing unittests for AEM (part 3): Mocking resources – Things on a content management system

View solution in original post

2 Replies
Jörg_Hoh
Correct answer by
Employee
Employee

Hi,

how does your testclass look like? Remember, that if you just call the constructor of your model class and then call methods on it, the references and injections are not resolved.

Please see [1], [2] and [3] for references how to create unittests with sling models and SlingContext/AemContext.

HTH,

Jörg

[1] Writing unit tests for AEM — using SlingMocks – Things on a content management system

[2] Writing unit tests for AEM (part 2): Maven Setup – Things on a content management system

[3] Writing unittests for AEM (part 3): Mocking resources – Things on a content management system

Arun_Patidar
Community Advisor
Community Advisor

I have a sample Unit Test code to test the Sling Model with Services.

I will share it with you for reference.