Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » problem with table viewer comparator(null elements while sorting after an addition)
problem with table viewer comparator [message #639456] Tue, 16 November 2010 16:16 Go to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Hi all,
I've got a problem with a table viewer that I really cannot understand: the following snippet when run on my system produces a null pointer exception after the first element addition in the table viewer:

public class View extends ViewPart {
	public static final String ID = "RCPEXAMPLE.view";

	private TableViewer viewer;

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	

	
	
	class Comparator extends ViewComparator{

	    public Comparator(ViewRegistry reg) {
		super(reg);
		// TODO Auto-generated constructor stub
	    }

	    /* (non-Javadoc)
	     * @see org.eclipse.ui.internal.dialogs.ViewComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	     */
	    @Override
	    public int compare(Viewer viewer, Object e1, Object e2) {
		String s1 = (String) e1;
		String s2 = (String) e2;
		System.out.println("Comparing " + s1 + " against " + s2);
		return s1.compareTo(s2);
	    }
	    
	}
	
	
	class ViewContentProvider implements IStructuredContentProvider{
	    private List<String> content = null;
	    
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		    if( newInput instanceof List ){
			content = (List<String>) newInput;
			for(String s : content )
			    ((TableViewer) v).add(s);
			    
		    }
		    
		    v.refresh();
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			return (content != null ? content.toArray() : new String[]{"AA", "BB", "CC"});
		}
		

		
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {



	    
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.VIRTUAL
				| SWT.V_SCROLL);
		ViewContentProvider provider = new ViewContentProvider(  );
		viewer.setContentProvider( provider );
		viewer.setLabelProvider(new ViewLabelProvider());
		String[] contents = new String[]{" First", "Second", "third" };
		viewer.setInput( contents );
		viewer.setComparator( new Comparator( null ) );
		
		PlatformUI.getWorkbench().getDisplay().asyncExec( new Runnable(){

		    @Override
		    public void run() {
			int j = 10;
			LinkedList<String> newContent = new LinkedList<String>();
			newContent.add("ZZZ");
			newContent.add("FFF");
			newContent.add("GGG");
			newContent.add("AAA");
			
			viewer.setInput( newContent );
		    }
		    
		}
		);



	}

	public void setFocus() {
	    viewer.getControl().setFocus();
	    
	}

}


The above does the following:
- create a table viewer and fill it with an initial content
- add a single entry at the time forcing the comparator triggering

Now, after the first addition (ZZZ) there is a null pointer exception within the compare method. I've debugged a lot and found that the method AbstractTableviewer.indexForElement, even if computes right min/max, get a null object from the doGetItem(mid) even if the mid is, of course, within min and max (and so the item should be valid).
Am I doing something wrong?

Thanks.
Re: problem with table viewer comparator [message #639464 is a reply to message #639456] Tue, 16 November 2010 16:39 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
It seems to be a problem with the virtual table. Removing the virtual style bit from the table viewer makes the sorting working without any null value. I'm able to use it as a standard table (i.e., not virtual), but in the case the table is virtual is this a bug?
Re: problem with table viewer comparator [message #639534 is a reply to message #639464] Tue, 16 November 2010 19:44 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
This normally depends on your content provider and not if you use
SWT.VIRTUAL. You can use a ViewerComparator/ViewerFilter as long as you
use a standard content provider.

Tom

Am 16.11.10 17:39, schrieb Luca Ferrari:
> It seems to be a problem with the virtual table. Removing the virtual
> style bit from the table viewer makes the sorting working without any
> null value. I'm able to use it as a standard table (i.e., not virtual),
> but in the case the table is virtual is this a bug?
Previous Topic:StyledText Does Not Show Control Characters
Next Topic:CheckBox button in a TableColumn
Goto Forum:
  


Current Time: Tue Apr 23 17:11:27 GMT 2024

Powered by FUDForum. Page generated in 0.04830 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top