我正在使用IEventBroker将事件同步发送到其他视图
@Inject
private IEventBroker eventBroker;
发送方部分具有以下代码:
Map<String,List<TreeMap<Timestamp, Long>>> StatisticValue = new HashMap<String,List<TreeMap<Timestamp, Long>>>();
//some code to add values in statisticValue
eventBroker.send("value", StatisticValue);
接收器端具有以下代码段。
@Inject
@Optional
public void getEvent(@UIEventTopic("value")HashMap<String,List<TreeMap<Timestamp, Long>>> entriesForTs)
{
System.out.println("size"+entriesForTs.size());
}
我在接收端收到以下错误:
Exception while dispatching event org.osgi.service.event.Event [topic=value] to handler org.eclipse.e4.ui.internal.di.UIEventObjectSupplier$UIEventHandler@6d79dea2
!STACK 0
org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:63)
at org.eclipse.e4.ui.internal.di.UIEventObjectSupplier$UIEventHandler$1.run(UIEventObjectSupplier.java:49)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:180)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4688)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:205)
at org.eclipse.e4.ui.internal.di.UIEventObjectSupplier$UIEventHandler.handleEvent(UIEventObjectSupplier.java:47)
at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)
at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)
at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)
at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)
at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:80)
at com.xyz.abc.project.parts.StatisticsPart.processFiles(StatisticsPart.java:270)
at com.xyz.abc.project.parts.StatisticsPart$5.selectionChanged(StatisticsPart.java:205)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:164)
at org.eclipse.jface.util.SafeRunnable$1.run(SafeRunnable.java:128)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:162)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2188)
at org.eclipse.jface.viewers.StructuredViewer.handleSelect(StructuredViewer.java:1211)
at org.eclipse.jface.viewers.StructuredViewer$4.widgetSelected(StructuredViewer.java:1241)
at org.eclipse.jface.util.OpenStrategy.fireSelectionEvent(OpenStrategy.java:239)
at org.eclipse.jface.util.OpenStrategy.access$4(OpenStrategy.java:233)
at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:403)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:162)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
Caused by: java.lang.NullPointerException
at com.xyz.abc.project.parts.showGraph.getEvent(showGraph.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
... 48 more
我很困惑为什么我会收到此错误。我已经在发送方检查了StatisticValue的大小(大小= 2)。但是它显示了一些nullpointer异常。谁能帮帮我吗?
最佳答案
异常是在NullPointerException
方法中报告getEvent
,因为entriesForTs
为空。@UIEventTopic
处理代码似乎在首次使用事件处理程序时以空值调用一次(然后使用正确的值再次调用它)。因此,只需在您的getEvent
中检查是否为null。
更新:
如果解析参数类型存在某些问题,可以尝试使用org.osgi.service.event.Event
作为getEvent
参数的类型:
public void getEvent(@UIEventTopic("value") Event event)
{
data = event.getProperty(IEventBroker.DATA);
如果
IEventBroker.subscribe
拒绝工作,您也可以尝试@UIEventTopic
。更新2:
事件代理
send
(和post
)方法特别对待Map
,并假定它是包含要发送的值的映射。因此它将在地图中寻找IEventBroker.DATA
键。最好将地图包装在容器类中。