Search code examples
osgiaemosgi-bundle

Cardinal property value in OSGi Component in AEM


I'm writing an OSGi component with cardinal values associated with it.

I've written the following lines:

@Property(name="cardinalValue",cardinality=4,description="testing cardinality")
private String[] cardinalValue;

Then in the code I'm trying to print out the cardinal value:

for(String cardinal : cardinalValue){
  log.debug(cardinal);
}

Once I install and trigger method in my component in the OSGi bundle, it appears that there is a null pointer exception even after I fill the values in the configuration manager console in AEM console.

It is the first time that I'm working with cardinality and I have even tried the List and Vector approach same as the array approach above, but still wasn't successful in printing the values in the log.


Solution

  • I would suggest you to initialize your property yourself.

    For this change current property declaration in next way:

    @Property(label="cardinalValue",cardinality=4,description="testing cardinality")
    private static final String CARDINAL_PROPERTY = "cardinalValue";
    private String[] cardinalValue;
    

    Then write method which will initialize property with values from configuration on component Activation and Modification:

    @Activate
    @Modified
    protected void activate(final Map<String, Object> props) {
        String[] cardinalValue = PropertiesUtil.toStringArray(props.get(CARDINAL_PROPERTY));
    }
    

    Now your cardinalValue variable will initialized.