Dima O
Dima O

Reputation: 83

bundleContext.getServiceReference in Osgi

I have a problem running a Java application, In the Applet Activator class when it opens the gui the bundleContext.getServiceReference throws a NPE, after some search in OSGi It came to me that it happen because there's no service registered that implements the UIService.. my questions is: what does it mean? what should I check to fix it?

error code:

java.lang.NullPointerException
at net.java.sip.communicator.impl.appletActivator.AppletActivator.openMainGui(AppletActivator.java:57)
at net.java.sip.communicator.impl.appletActivator.AppletActivator.start(AppletActivator.java:40)
at org.apache.felix.framework.util.SecureAction$Actions.run(SecureAction.java:1243)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:620)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
at java.lang.Thread.run(Unknown Source)

source:

public static <T> T getService(BundleContext bundleContext,
        Class<T> serviceClass) {
            //This throws the NullPointerException
    ServiceReference serviceReference = bundleContext 
            .getServiceReference(serviceClass.getName());

    return (serviceReference == null) ? null : (T) bundleContext
            .getService(serviceReference);
}

I'm new to OSGi and don't understand some of the concepts.

Upvotes: 0

Views: 3571

Answers (1)

BJ Hargrave
BJ Hargrave

Reputation: 9384

Is there some bundle installed in the framework that will register the UIService? Also, your code appears that it may be start order sensitive which is a no-no in OSGi. Calling your getService method and expecting it to return a service assumes that the bundle which registers the service has (a) already been started and (b) has registered the service. This is why you normally use DS or ServiceTrackers in OSGi applications to decouple the start order of bundles.

Upvotes: 1

Related Questions