If you are getting the the NPE whle creating the TableViewer and your stack trace looks somewhat like this:
java.lang.NullPointerException
at org.eclipse.jface.viewers.ViewerColumn.refresh(ViewerColumn.java:145)
at org.eclipse.jface.viewers.AbstractTableViewer.doUpdateItem(AbstractTableViewer.java:399)
at org.eclipse.jface.viewers.StructuredViewer$UpdateItemSafeRunnable.run(StructuredViewer.java:481)
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.StructuredViewer.updateItem(StructuredViewer.java:2111)
at org.eclipse.jface.viewers.AbstractTableViewer.createItem(AbstractTableViewer.java:277)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefreshAll(AbstractTableViewer.java:757)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:649)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:636)
at org.eclipse.jface.viewers.AbstractTableViewer$2.run(AbstractTableViewer.java:592)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1392)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1353)
at org.eclipse.jface.viewers.AbstractTableViewer.inputChanged(AbstractTableViewer.java:590)
at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:274)
at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1634)
Than you encountered a very silly problem, and generally silly problems are hard to debug which is exactly the case with above exception. There are two ways of setting the label provider on your TableViewer component as follows:
Scenario 1:
1. Creating JFace TableViewer and calling setLabelProvider on it
2. Creating your swt TableColumns
Scenario 2:
1. Creating JFace TableViewer
2. Creating JFace TableViewerColumn and set ColumnLabelProvider on it
My colleague faced the problem in Scenario 1 and asked me to help him in it. As he told me that he was debugging from some time now and the problem gets resolved if Scenario 1: is changed to:
Scenario 1(modified):
java.lang.NullPointerException
at org.eclipse.jface.viewers.ViewerColumn.refresh(ViewerColumn.java:145)
at org.eclipse.jface.viewers.AbstractTableViewer.doUpdateItem(AbstractTableViewer.java:399)
at org.eclipse.jface.viewers.StructuredViewer$UpdateItemSafeRunnable.run(StructuredViewer.java:481)
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.StructuredViewer.updateItem(StructuredViewer.java:2111)
at org.eclipse.jface.viewers.AbstractTableViewer.createItem(AbstractTableViewer.java:277)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefreshAll(AbstractTableViewer.java:757)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:649)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:636)
at org.eclipse.jface.viewers.AbstractTableViewer$2.run(AbstractTableViewer.java:592)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1392)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1353)
at org.eclipse.jface.viewers.AbstractTableViewer.inputChanged(AbstractTableViewer.java:590)
at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:274)
at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1634)
Than you encountered a very silly problem, and generally silly problems are hard to debug which is exactly the case with above exception. There are two ways of setting the label provider on your TableViewer component as follows:
Scenario 1:
1. Creating JFace TableViewer and calling setLabelProvider on it
2. Creating your swt TableColumns
Scenario 2:
1. Creating JFace TableViewer
2. Creating JFace TableViewerColumn and set ColumnLabelProvider on it
My colleague faced the problem in Scenario 1 and asked me to help him in it. As he told me that he was debugging from some time now and the problem gets resolved if Scenario 1: is changed to:
Scenario 1(modified):
1. Creating JFace TableViewer
2. Creating your swt TableColumn
3. Call setLabelProvider on TableViewer
I hope this will solve your problem.
Enjoy coding :-)
Thank you !!!!!!!!!!!
ReplyDelete