javaeclipseeclipse-rcpnattablenebula

NullPointerException when creating a NatTable object in an Eclipse RCP part


I'm learning about Nebula NatTable. I created an RCP app. In the part, I edited the @PostConstruct like this:

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

But when running the app. I got the exception. Can you clear me?

Update: adding the exception log:

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$7.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$0(PartRenderingEngine.java:743)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$2.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$5.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)

Update: this code was run well:

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);
    }
}

Update: the solution: don't use the style bit as Dirk Fauth said:

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

Solution

  • The short answer is, because you are doing it wrong. Please read our Getting Started Tutorial to learn how to use NatTable.

    There are at least 3 errors in your code:

    1. You are not providing a layer to use, which results in using the DummyGridLayerStack which does not help you in any way.
    2. You specify your own style options. That should only be done if you know what you are doing.
    3. You disabled the default configuration but don't provide an alternative.

    I suppose 2. is the reason for the NullPointerException because you do not specify the style options for scrollbars, but using the DummyGridLayerStack there is the ViewportLayer which needs the reference to the scrollbars to actually support scrolling.

    Although 3. could be the cause for the NullPointerException because the default configuration is disabled and no alternative configuration is set.