问题
An E4 RCP application consists on several nattables.
Material nattable allows to create new user defined materials.
Material nattable includes filter columns.
EventList<AncolabMaterial> eventList = GlazedLists.eventList(input_values_list);
TransformedList<?, ?> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
SortedList<?> sortedList = new SortedList<>(rowObjectsGlazedList, null);
this.filterList = new FilterList<AncolabMaterial>((EventList<AncolabMaterial>) sortedList);
this.bodyDataProvider = new ListDataProvider<AncolabMaterial>(this.filterList, columnPropertyAccessor);
DataLayer bodyDataLayer = new DataLayer(getBodyDataProvider());
bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(((ListDataProvider<AncolabMaterial>) bodyDataProvider).getList()));
bodyDataLayer.registerCommandHandler(new AddRowCommandHandler(((ListDataProvider<Object>) bodyDataProvider).getList()));
// layer for event handling of GlazedLists and PropertyChanges
GlazedListsEventLayer<?> glazedListsEventLayer =
new GlazedListsEventLayer<AncolabMaterial>(bodyDataLayer, this.filterList);
this.selectionLayer = new SelectionLayer(glazedListsEventLayer, false);
selectionLayer.addConfiguration(new DefaultRowSelectionLayerConfiguration());
ViewportLayer viewportLayer = new ViewportLayer(getSelectionLayer());
setUnderlyingLayer(viewportLayer);
/...
This is done by creating a command and command handler to process add events as in link (Delete rows from Nattable)
- Creating a command to add a row
public class AddRowCommand extends AbstractRowCommand {
private Object item;
public AddRowCommand(ILayer layer, Object item) {
super(layer, 0);
this.setItem(item);
}
protected AddRowCommand(AddRowCommand command) {
super(command);
}
@Override
public ILayerCommand cloneCommand() {
return new AddRowCommand(this);
}
public Object getItem() {
return this.item;
}
private void setItem(Object item) {
this.item = item;
}
}
- Create a command handler
public class AddRowCommandHandler implements ILayerCommandHandler<AddRowCommand> {
private List<Object> bodyData;
public AddRowCommandHandler(List<Object> bodyData) {
this.bodyData = bodyData;
}
@Override
public Class<AddRowCommand> getCommandClass() {
return AddRowCommand.class;
}
@Override
public boolean doCommand(ILayer targetLayer, AddRowCommand command) {
this.bodyData.add(0, (AncolabMaterial)command.getItem() );
targetLayer.fireLayerEvent(new RowInsertEvent(targetLayer, command.getRowPosition()));
return true;
}
}
- Register the command handler to the body DataLayer
bodyDataLayer.registerCommandHandler(new DeleteRowCommandHandler<>(((ListDataProvider<AncolabMaterial>) bodyDataProvider).getList()));
bodyDataLayer.registerCommandHandler(new AddRowCommandHandler(((ListDataProvider<Object>) bodyDataProvider).getList()));
Note: command handler for deleting rows works fine
- Add a menu item to your menu configuration that fires the command
//At SWT Button mouseUp event:
AddUserMaterialDialog dialog = new AddUserMaterialDialog(shell);
dialog.create();
if (dialog.open() == Window.OK) {
AncolabMaterial matToAdd = dialog.getUserMaterial();
natTableMaterials.doCommand(new AddRowCommand(natTableMaterials, matToAdd));
}
After creating new material, the list of materials is updated but when rendering the table the following error is thrown and the table does not render properly:
Error while painting table
java.lang.RuntimeException: java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getDataValue(ReflectiveColumnPropertyAccessor.java:74)
at org.eclipse.nebula.widgets.nattable.data.ListDataProvider.getDataValue(ListDataProvider.java:51)
at org.eclipse.nebula.widgets.nattable.layer.DataLayer.getDataValue(DataLayer.java:164)
at org.eclipse.nebula.widgets.nattable.layer.DataLayer.getDataValueByPosition(DataLayer.java:447)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getDataValueByPosition(AbstractIndexLayerTransform.java:358)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getDataValueByPosition(AbstractLayerTransform.java:351)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getDataValueByPosition(CompositeLayer.java:617)
at org.eclipse.nebula.widgets.nattable.layer.cell.AbstractLayerCell.getDataValue(AbstractLayerCell.java:64)
at org.eclipse.nebula.widgets.nattable.layer.cell.CellDisplayConversionUtils.convertDataType(CellDisplayConversionUtils.java:21)
at org.eclipse.nebula.widgets.nattable.painter.cell.AbstractTextPainter.convertDataType(AbstractTextPainter.java:188)
at org.eclipse.nebula.widgets.nattable.painter.cell.TextPainter.paintCell(TextPainter.java:157)
at org.eclipse.nebula.widgets.nattable.painter.cell.CellPainterWrapper.paintCell(CellPainterWrapper.java:67)
at org.eclipse.nebula.widgets.nattable.painter.cell.decorator.LineBorderDecorator.paintCell(LineBorderDecorator.java:76)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintCell(CellLayerPainter.java:216)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintLayer(CellLayerPainter.java:86)
at org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter.paintLayer(GridLineCellLayerPainter.java:141)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter.paintLayer(SelectionLayerPainter.java:111)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer$CompositeLayerPainter.paintLayer(CompositeLayer.java:1055)
at org.eclipse.nebula.widgets.nattable.painter.layer.NatLayerPainter.paintLayer(NatLayerPainter.java:54)
at org.eclipse.nebula.widgets.nattable.NatTable.paintNatTable(NatTable.java:507)
at org.eclipse.nebula.widgets.nattable.NatTable.paintControl(NatTable.java:502)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:231)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4118)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1052)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1076)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
at org.eclipse.swt.widgets.Composite.WM_PAINT(Composite.java:1561)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4822)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:345)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4765)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2303)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3532)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1170)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1059)
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:656)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:592)
at org.eclipse.equinox.launcher.Main.run(Main.java:1498)
at org.eclipse.equinox.launcher.Main.main(Main.java:1471)
Caused by: java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getPropertyDescriptor(ReflectiveColumnPropertyAccessor.java:107)
at org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor.getDataValue(ReflectiveColumnPropertyAccessor.java:69)
... 53 more
回答1:
The NPE is coming from the ReflectiveColumnPropertyAccessor
when trying to access a getter method via reflection on a null object. I suppose you are running into issues with the list transformations. If I read your code correctly you are using the FilterList
in your command handler. The add operation on the FilterList
is not or not yet propagated to the underlying input list. I would suggest to register the command handlers with the EventList
and not the FilterList
.
来源:https://stackoverflow.com/questions/53080736/nattable-add-row-command