OSGI EventAdmin无法发送事件-生产者上出现空点异常

时间:2019-08-26 20:15:06

标签: osgi osgi-bundle

我正在尝试向消费者发送事件。我收到NullPointerException

这是我的活动代码

public class Activator implements BundleActivator {

    EventAdmin eventAdmin;
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("started");
        Dictionary<String, String> properties = new Hashtable<>();
        properties.put("test", "blah");
        Event event = new Event("test/sent", properties);
        System.out.println("sending");
        eventAdmin.sendEvent(event);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("stopped ");
    }
} 

这是日志

  

org.osgi.framework.BundleException:中的异常   套件h的com.aml.project.Activator.start()。在   org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:863)     在   org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:791)     在   org.eclipse.osgi.internal.framework.EquinoxBundle.startWorker0(EquinoxBundle.java:1013)     在   org.eclipse.osgi.internal.framework.EquinoxBundle $ EquinoxModule.startWorker(EquinoxBundle.java:365)     在org.eclipse.osgi.container.Module.doStart(Module.java:598)处   org.eclipse.osgi.container.Module.start(Module.java:462)在   org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:439)     在org.apache.felix.gogo.command.Basic.start(Basic.java:739)处   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:567)在   org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:139)     在   org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:91)     在org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:599)     在   org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:526)     在org.apache.felix.gogo.runtime.Closure.execute(Closure.java:415)     在org.apache.felix.gogo.runtime.Pipe.doCall(Pipe.java:416)处   org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:229)在   org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:59)在   java.base / java.util.concurrent.FutureTask.run(FutureTask.java:264)在   java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)     在   java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628)     在java.base / java.lang.Thread.run(Thread.java:835)造成原因:   java.lang.NullPointerException在   com.aml.project.Activator.start(Activator.java:21)位于   org.eclipse.osgi.internal.framework.BundleContextImpl $ 3.run(BundleContextImpl.java:842)     在   org.eclipse.osgi.internal.framework.BundleContextImpl $ 3.run(BundleContextImpl.java:1)     在   java.base / java.security.AccessController.doPrivileged(AccessController.java:552)     在   org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:834)     ... 23更多java.lang.NullPointerException

1 个答案:

答案 0 :(得分:3)

您使用从未设置的eventAdmin字段。这样就得到了NullPointerException。我建议改用声明式服务,并使用@Reference注入EventAdmin。

有关所有详细信息,请参见Vogella的OSGi Event Admin – Publish & Subscribe