Expand my Community achievements bar.

SOLVED

AEM 6.5 - References in service class are null when extended

Avatar

Level 1

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
    }
}

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

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.

Avatar

Correct answer by
Employee Advisor

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

Avatar

Level 4

Hi @Raghul87 ,

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

Thanks!