[SOLVED] "@Reference ConfigurationAdmin configAdmin" gives null

I am trying to make my binding automatically initializing and saving own configuration, as described in https://www.openhab.org/docs/developer/osgi/configadmin.html , but Eclipse fails to build my code with “Non-null reference may have not been initialized”. What am i doing wrong ?

Without seeing your code, it’s a bit difficult…


I am adding the annotated field to DeviRegHandlerFactory class

@vzorglub I’ve shown you the code. The problem is still there

I am no dev. But the guys that know about that will be able to help now.
Be patient, we are all volunteers and have lifes outside openHAB

Hi Pavel,

Assuming your DeviRegHandlerFactory class is annoted with @NonNullByDefault the compiler expects the field to be initialized. It does not know about that, if you reference an OSGi service, OSGi will already make sure that the field is non-null. You must therefore disable null-checks for such references:

@Reference
private @NonNullByDefault({}) MyService injectedService;

See also https://www.openhab.org/docs/developer/guidelines.html#null-annotations. I will update the docs about the ConfigurationAdmin and add nullness annotations to the example code.

Thank you very much for help. Seems to be solved.
This nullness check in Eclipse is very annoying. I hate being force-taught to code by an IDE. Or i cannot cook it right :wink:

Sometimes it is tough e.g. if superclasses are not annotated yet …

I updated the example code: