Error while compiling a serivce | Community
Skip to main content
November 19, 2019
Solved

Error while compiling a serivce

  • November 19, 2019
  • 2 replies
  • 1075 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

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

2 replies

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
November 19, 2019

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

arunpatidar
Community Advisor
Community Advisor
November 21, 2019

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

I will share it with you for reference.

Arun Patidar