AEM 6.5 - References in service class are null when extended | Community
Skip to main content
New Participant
Nov 5, 2021 3:43 PM UTC
Solved

AEM 6.5 - References in service class are null when extended

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.
joerghoh
joerghohAccepted solution
Employee
Nov 5, 2021 9:58 PM UTC

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

MohitKumarK
Level 3
Nov 6, 2021 9:44 AM UTC

Hi @raghul87 ,

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

Thanks!

Translate
joerghoh
joerghohAccepted solution
Employee
Nov 5, 2021 9:58 PM UTC

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

Translate
Vijayalakshmi_S
Level 10
Nov 5, 2021 7:45 PM UTC

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.

Translate