在 Eclipse RCP 部件中创建 NatTable 对象时出现 NullPointerException

NullPointerException when creating a NatTable object in an Eclipse RCP part

我正在了解 Nebula NatTable。我创建了一个 RCP 应用程序。在这一部分,我像这样编辑了@PostConstruct:

    @PostConstruct
    public void createComposite(Composite parent) {
        final NatTable natTable = new NatTable(parent, SWT.NO_BACKGROUND
                | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED, false);
    }

但是当 运行安装应用程序时。我得到了例外。你能清除我吗?

更新:添加异常日志:

java.lang.NullPointerException
    at org.eclipse.nebula.widgets.nattable.viewport.ScrollBarScroller.addListener(ScrollBarScroller.java:36)
    at org.eclipse.nebula.widgets.nattable.viewport.ScrollBarHandlerTemplate.<init>(ScrollBarHandlerTemplate.java:64)
    at org.eclipse.nebula.widgets.nattable.viewport.HorizontalScrollBarHandler.<init>(HorizontalScrollBarHandler.java:34)
    at org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer.doCommand(ViewportLayer.java:1010)
    at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.doCommand(AbstractIndexLayerTransform.java:109)
    at org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer.doCommandOnChildLayer(GridLayer.java:113)
    at org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer.doCommandOnChildLayers(GridLayer.java:99)
    at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.doCommand(CompositeLayer.java:151)
    at org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer.doCommand(GridLayer.java:187)
    at org.eclipse.nebula.widgets.nattable.NatTable.doCommand(NatTable.java:820)
    at org.eclipse.nebula.widgets.nattable.NatTable.handleEvent(NatTable.java:475)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1103)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1084)
    at org.eclipse.swt.widgets.Control.WM_SIZE(Control.java:5511)
    at org.eclipse.swt.widgets.Scrollable.WM_SIZE(Scrollable.java:367)
    at org.eclipse.swt.widgets.Composite.WM_SIZE(Composite.java:1763)
    at org.eclipse.swt.widgets.Canvas.WM_SIZE(Canvas.java:492)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4918)
    at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:359)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
    at org.eclipse.swt.widgets.Scrollable.callWindowProc(Scrollable.java:88)
    at org.eclipse.swt.widgets.Control.WM_WINDOWPOSCHANGED(Control.java:5743)
    at org.eclipse.swt.widgets.Canvas.WM_WINDOWPOSCHANGED(Canvas.java:499)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4931)
    at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:359)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.SetWindowPos(Native Method)
    at org.eclipse.swt.widgets.Widget.SetWindowPos(Widget.java:1482)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3304)
    at org.eclipse.swt.widgets.Composite.setBoundsInPixels(Composite.java:1079)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3265)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3261)
    at org.eclipse.swt.widgets.Control.setBounds(Control.java:3256)
    at org.eclipse.swt.layout.FillLayout.layout(FillLayout.java:219)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1363)
    at org.eclipse.swt.widgets.Composite.WM_SIZE(Composite.java:1774)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4918)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
    at org.eclipse.swt.widgets.Scrollable.callWindowProc(Scrollable.java:88)
    at org.eclipse.swt.widgets.Control.WM_WINDOWPOSCHANGED(Control.java:5743)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4931)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.EndDeferWindowPos(Native Method)
    at org.eclipse.swt.widgets.Composite.resizeChildren(Composite.java:1007)
    at org.eclipse.swt.widgets.Composite.resizeChildren(Composite.java:973)
    at org.eclipse.swt.widgets.Composite.setResizeChildren(Composite.java:1187)
    at org.eclipse.swt.widgets.Composite.WM_SIZE(Composite.java:1778)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4918)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
    at org.eclipse.swt.widgets.Scrollable.callWindowProc(Scrollable.java:88)
    at org.eclipse.swt.widgets.Control.WM_WINDOWPOSCHANGED(Control.java:5743)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4931)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.SetWindowPos(Native Method)
    at org.eclipse.swt.widgets.Widget.SetWindowPos(Widget.java:1482)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3304)
    at org.eclipse.swt.widgets.Composite.setBoundsInPixels(Composite.java:1079)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3265)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3261)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3337)
    at org.eclipse.swt.widgets.Control.setBounds(Control.java:3333)
    at org.eclipse.e4.ui.workbench.renderers.swt.SashLayout.setRectangle(SashLayout.java:283)
    at org.eclipse.e4.ui.workbench.renderers.swt.SashLayout.tileSubNodes(SashLayout.java:228)
    at org.eclipse.e4.ui.workbench.renderers.swt.SashLayout.tileSubNodes(SashLayout.java:271)
    at org.eclipse.e4.ui.workbench.renderers.swt.SashLayout.layout(SashLayout.java:145)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1363)
    at org.eclipse.swt.widgets.Composite.WM_SIZE(Composite.java:1774)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4918)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
    at org.eclipse.swt.widgets.Scrollable.callWindowProc(Scrollable.java:88)
    at org.eclipse.swt.widgets.Control.WM_WINDOWPOSCHANGED(Control.java:5743)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4931)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.SetWindowPos(Native Method)
    at org.eclipse.swt.widgets.Widget.SetWindowPos(Widget.java:1482)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3304)
    at org.eclipse.swt.widgets.Composite.setBoundsInPixels(Composite.java:1079)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3265)
    at org.eclipse.swt.widgets.Control.setBoundsInPixels(Control.java:3261)
    at org.eclipse.swt.widgets.Control.setBounds(Control.java:3256)
    at org.eclipse.swt.layout.FillLayout.layout(FillLayout.java:204)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1363)
    at org.eclipse.swt.widgets.Composite.WM_SIZE(Composite.java:1774)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4918)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
    at org.eclipse.swt.widgets.Scrollable.callWindowProc(Scrollable.java:88)
    at org.eclipse.swt.widgets.Control.WM_WINDOWPOSCHANGED(Control.java:5743)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4931)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
    at org.eclipse.swt.internal.win32.OS.EndDeferWindowPos(Native Method)
    at org.eclipse.swt.widgets.Composite.resizeChildren(Composite.java:1007)
    at org.eclipse.swt.widgets.Composite.resizeChildren(Composite.java:973)
    at org.eclipse.swt.widgets.Composite.setResizeChildren(Composite.java:1187)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1364)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1348)
    at org.eclipse.swt.widgets.Composite.setLayoutDeferred(Composite.java:1147)
    at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.forceLayout(WBWRenderer.java:941)
    at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.postProcess(WBWRenderer.java:782)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:684)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:772)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access[=11=](PartRenderingEngine.java:743)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:737)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:721)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1081)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1044)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
    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:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
    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:653)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1472)

更新:这段代码运行很好:

public class SamplePart {

    @PostConstruct
    public void createComposite(Composite parent) {
        final NatTable natTable = new NatTable(parent, SWT.NO_BACKGROUND
                | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED, false);
        final DataLayer bodyDataLayer = new DataLayer(new IDataProvider() {

            @Override
            public void setDataValue(int columnIndex, int rowIndex, Object newValue) { }

            @Override
            public int getRowCount() { return 0; }

            @Override
            public Object getDataValue(int columnIndex, int rowIndex) { return null; }

            @Override
            public int getColumnCount() { return 0; }
        });
        natTable.setLayer(bodyDataLayer);
    }
}

更新:解决方案:不要像 Dirk Fauth 所说的那样使用样式位:

final NatTable natTable = new NatTable(parent, false);

简短的回答是,因为你做错了。请阅读我们的 Getting Started Tutorial 以了解如何使用 NatTable。

您的代码中至少有 3 个错误:

  1. 您没有提供要使用的层,这导致使用 DummyGridLayerStack 对您没有任何帮助。
  2. 您指定自己的样式选项。只有当你知道自己在做什么时才应该这样做。
  3. 您禁用了默认配置,但未提供替代配置。

我想 2. 是 NullPointerException 的原因,因为您没有指定滚动条的样式选项,但是使用 DummyGridLayerStackViewportLayer 需要参考到滚动条以实际支持滚动。

尽管 3. 可能是 NullPointerException 的原因,因为默认配置已禁用且未设置替代配置。