OSGI EventAdmin 无法发送事件 - 在生产者上获取空点异常



我正在尝试向消费者发送事件。我收到空指针异常

这是我的事件代码

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: Exception in com.aml.project.Activator.start(( 的捆绑包 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( at org.eclipse.osgi.container.Module.doStart(Module.java:598( at org.eclipse.osgi.container.Module.start(Module.java:462( at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:439( at org.apache.felix.gogo.command.Basic.start(Basic.java:739( at 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( at java.base/java.lang.reflect.Method.invoke(Method.java:567( at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:139( 在 org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:91( at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:599( 在 org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:526( at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:415( at org.apache.felix.gogo.runtime.Pipe.doCall(Pipe.java:416( at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:229( at org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:59( at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264( at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128( 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628( at java.base/java.lang.Thread.run(Thread.java:835( 由以下原因引起: java.lang.NullPointerException at com.aml.project.Activator.start(Activator.java:21( at 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 more java.lang.NullPointerException

您使用从未设置的字段 eventAdmin。所以你会得到一个 NullPointerException。我建议改用声明性服务,并使用@Reference注入事件管理员。

请参阅 OSGi Event Admin – Publish & Subscribe from Vogella,了解所有详细信息。

相关内容

最新更新