NullPointerException при создании объекта NatTable в части RCP Eclipse

Я изучаю туманность 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$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)

Обновление: этот код был хорошо выполнен:

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

Обновление: решение: не используйте бит стиля, как сказал Дирк Фаут:

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

1 ответ

Решение

Краткий ответ, потому что вы делаете это неправильно. Пожалуйста, ознакомьтесь с нашим руководством по началу работы, чтобы узнать, как использовать NatTable.

В вашем коде как минимум 3 ошибки:

  1. Вы не предоставляете слой для использования, что приводит к использованию DummyGridLayerStack который вам никак не поможет.
  2. Вы указываете свои собственные параметры стиля. Это должно быть сделано, только если вы знаете, что делаете.
  3. Вы отключили конфигурацию по умолчанию, но не предоставили альтернативы.

Я полагаю, 2. является причиной NullPointerException потому что вы не указываете параметры стиля для полос прокрутки, но с помощью DummyGridLayerStack Здесь ViewportLayer которая нуждается в ссылке на полосы прокрутки, чтобы фактически поддерживать прокрутку.

Хотя 3. может быть причиной для NullPointerException потому что конфигурация по умолчанию отключена и альтернативная конфигурация не установлена.

Другие вопросы по тегам