【问题标题】:OSGI EventAdmin can not send event - getting null point exception on ProducerOSGI EventAdmin 无法发送事件 - 在 Producer 上获取空点异常
【发布时间】:2019-12-31 00:41:18
【问题描述】:

我正在尝试向消费者发送事件。我收到 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: 异常 com.aml.project.Activator.start() 的 bundle h。在 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(Native 方法)在 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

【问题讨论】:

    标签: osgi osgi-bundle


    【解决方案1】:

    您使用从未设置的字段 eventAdmin。所以你得到一个 NullPointerException。我建议改用声明式服务并使用@Reference 注入 EventAdmin。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多