Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 249514 - [Viewers] [JFace] NPE setting StructuredSelection(Object[])
Summary: [Viewers] [JFace] NPE setting StructuredSelection(Object[])
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.5 M3   Edit
Assignee: Boris Bokowski CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-02 11:52 EDT by Dan Heidinga CLA
Modified: 2009-06-03 13:21 EDT (History)
1 user (show)

See Also:


Attachments
Assert added and javadoc updated (1006 bytes, patch)
2008-10-02 11:53 EDT, Dan Heidinga CLA
bokowski: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dan Heidinga CLA 2008-10-02 11:52:34 EDT
Build ID:  I20080617-2000

Steps To Reproduce:
1. Pass null to new StructuredSelection(Object[])
2. Watch NPE occur

More information:
The problem is there is no check on the the constructor for a null array.  The javadoc doesn't specify that null is an illegal value, but looking at the class, it appears the other constructors do not allow null.

Proposed fix:
  * Update the javadoc to say null is not allowed
  * Add an Assert.isNotNull to the constructor to match other constructors.

Stacktrace:
Thread [main] (Suspended (exception NullPointerException))	
	StructuredSelection.<init>(Object[]) line: 61	
	CategoriesView(SmalltalkBrowsingPart).selectAll(boolean) line: 864	
	CategoriesView.setSelection(IStructuredSelection, boolean) line: 274	
	CategoriesView(SmalltalkBrowsingPart).adjustInputAndSetSelection(Object) line: 797	
	CategoriesView.adjustInputAndSetSelection(Object) line: 253	
	CategoriesView(SmalltalkBrowsingPart).selectionChanged(IWorkbenchPart, ISelection) line: 731	
	CategoriesView.selectionChanged(IWorkbenchPart, ISelection) line: 224	
	PageSelectionService(AbstractSelectionService).firePostSelection(IWorkbenchPart, ISelection) line: 179	
	AbstractSelectionService$2.selectionChanged(SelectionChangedEvent) line: 71	
	StructuredViewer$3.run() line: 842	
	SafeRunner.run(ISafeRunnable) line: 37	
	Platform.run(ISafeRunnable) line: 880	
	JFaceUtil$1.run(ISafeRunnable) line: 48	
	SafeRunnable.run(ISafeRunnable) line: 175	
	TreeViewer(StructuredViewer).firePostSelectionChanged(SelectionChangedEvent) line: 840	
	TreeViewer(StructuredViewer).handlePostSelect(SelectionEvent) line: 1153	
	StructuredViewer$5.widgetSelected(SelectionEvent) line: 1178	
	OpenStrategy.firePostSelectionEvent(SelectionEvent) line: 251	
	OpenStrategy.access$5(OpenStrategy, SelectionEvent) line: 245	
	OpenStrategy$3.run() line: 419	
	RunnableLock.run() line: 35	
	UISynchronizer(Synchronizer).runAsyncMessages(boolean) line: 133	
	Display.runAsyncMessages(boolean) line: 3800	
	Display.readAndDispatch() line: 3425	
	Workbench.runEventLoop(Window$IExceptionHandler, Display) line: 2382	
	Workbench.runUI() line: 2346	
	Workbench.access$4(Workbench) line: 2198	
	Workbench$5.run() line: 493	
	Realm.runWithDefault(Realm, Runnable) line: 288	
	Workbench.createAndRunWorkbench(Display, WorkbenchAdvisor) line: 488	
	PlatformUI.createAndRunWorkbench(Display, WorkbenchAdvisor) line: 149	
	IDEApplication.start(IApplicationContext) line: 113	
	EclipseAppHandle.run(Object) line: 193	
	EclipseAppLauncher.runApplication(Object) line: 110	
	EclipseAppLauncher.start(Object) line: 79	
	EclipseStarter.run(Object) line: 382	
	EclipseStarter.run(String[], Runnable) line: 179	
	NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
	NativeMethodAccessorImpl.invoke(Object, Object[]) line: 45	
	DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25	
	Method.invoke(Object, Object...) line: 612	
	Main.invokeFramework(String[], URL[]) line: 549	
	Main.basicRun(String[]) line: 504	
	Main.run(String[]) line: 1236	
	Main.main(String[]) line: 1212
Comment 1 Dan Heidinga CLA 2008-10-02 11:53:59 EDT
Created attachment 114113 [details]
Assert added and javadoc updated

Patch makes the StructuredSelection(Object[]) constructor consistent with the others.
Comment 2 Boris Bokowski CLA 2008-10-03 12:53:45 EDT
(In reply to comment #0)
> The
> javadoc doesn't specify that null is an illegal value, but looking at the
> class, it appears the other constructors do not allow null.

See http://www.eclipse.org/articles/article.php?file=Article-API-Use/index.html
"Do not treat null as an object. Null is more the lack of an object. Assume everything is non-null unless the API specification says otherwise."

I don't think we need to do anything here. Yes, throwing a different exception would be clearer, but we fail fast, so it is not like this would lead to potentially hard to find bugs. (This is also why we have the asserts in the other constructors, where we would not fail fast otherwise.)
Comment 3 Dan Heidinga CLA 2008-10-03 13:06:25 EDT
(In reply to comment #2)
> (In reply to comment #0)
> > The
> > javadoc doesn't specify that null is an illegal value, but looking at the
> > class, it appears the other constructors do not allow null.
> 
> See http://www.eclipse.org/articles/article.php?file=Article-API-Use/index.html
> "Do not treat null as an object. Null is more the lack of an object. Assume
> everything is non-null unless the API specification says otherwise."

Sure - I'm not disagreeing that null is invalid, I'm asking that the javadoc be updated to match the javadoc in some of other constructors.  Using Eclipse, you can easily see the javadoc while developing, but you won't necessarily remember things from the article.

> 
> I don't think we need to do anything here. Yes, throwing a different exception
> would be clearer, but we fail fast, so it is not like this would lead to
> potentially hard to find bugs. (This is also why we have the asserts in the
> other constructors, where we would not fail fast otherwise.)
> 

I agree with the Asserts - they say "I know this is an invalid input and I've marked it as such".  An NPE, on the other hand, says something is wrong - it may be in my code or it may be in Eclipse.  Making the change removes an ambiguity.

Comment 4 Boris Bokowski CLA 2008-10-03 13:08:37 EDT
If you insist...
Comment 5 Boris Bokowski CLA 2008-10-03 13:57:18 EDT
We would have to change a lot of JavaDoc if we wanted to fix this consistently. Not sure about the cost/benefit ratio though.

Don't get me wrong, I do agree with your points and would recommend making constraints explicit in the Javadoc. Having to rely on an external "how to use the API" article does not work very well.
Comment 6 Boris Bokowski CLA 2008-10-28 16:58:50 EDT
Verified by code inspection using I20081027-1300.
Comment 7 Boris Bokowski CLA 2009-06-03 13:21:37 EDT
Comment on attachment 114113 [details]
Assert added and javadoc updated

added missing iplog+ flag.