AEM 6.5 - References in service class are null when extended | Community
Skip to main content
November 5, 2021
Solved

AEM 6.5 - References in service class are null when extended

  • November 5, 2021
  • 3 replies
  • 2092 views

Am running application in AEM 6.5. I wrote service class with some References and uses SCR annotations. When extending this class and try to call the methods in the parent class the reference are null throwing Null Pointer Exception.

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class BaseClass implements IBase {

    @Reference
    protected HelperServiceImpl helperServiceImpl;

    protected void someMethod() {
        helperServiceImpl.getHelp(); //Here the helperServiceImpl is null when called from the extended class
    }
}

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class ExtendedClass extends BaseClass implements IExtend {

    protected void someMethod2() {
        helperServiceImpl.getHelp(); //Call HelperServiceImpl class reference which is present in parent class
    }
}

 

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

SCR and OSGI Annotations are not inherited. 

 

You can find a good explanation why this isn't the case here: https://github.com/osgi/osgi.enroute.site/blob/pre-R7/_faq/ds-inheritance.md

3 replies

Vijayalakshmi_S
Level 10
November 5, 2021

Hi @raghul87,

Cross check if HelperServiceImpl  is an OSGI service, annotated with @Service. (Per the naming convention, am assuming it to be just a component and not declared as Service)

If it is a service and yet the reference is null, share HelperServiceImpl class if possible to debug further.

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
November 5, 2021

SCR and OSGI Annotations are not inherited. 

 

You can find a good explanation why this isn't the case here: https://github.com/osgi/osgi.enroute.site/blob/pre-R7/_faq/ds-inheritance.md

MohitKumarK
Level 3
November 6, 2021

Hi @raghul87 ,

I feel you need to inject BaseClass in your ExtendedClass. Inheritance might not work in services I guess.

Thanks!