Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 369954

Summary: NPE when adding a notifier
Product: [Modeling] EMF Reporter: Brian de Alwis <bsd>
Component: CoreAssignee: Ed Merks <Ed.Merks>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 2.8.0   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description Brian de Alwis CLA 2012-01-27 10:51:09 EST
Eclipse Version: 4.2.0
Build id: I20120126-1300
(EMF 2.8)

I apologize in advance as I'm not a strong EMF user.  I've hit an NPE with E4 and EMF (relevant stack trace below) where we attempt to add a notifier to our root MApplication object as follows:

	((Notifier) uiRoot).eAdapters().add(uiEventPublisher);

The NPE arises as our MApplication's toolBarContributions feature (a list of MToolBarContribution) has had a "null" entry:

   [org.eclipse.e4.ui.model.application.ui.menu.impl.ToolBarContributionImpl@6164bd1e (elementId: null, tags: null, contributorURI: null) (widget: null, renderer: null, toBeRendered: true, onTop: false, visible: true, containerData: null, accessibilityPhrase: null) (parentId: mail.view, positionInParent: after=additions), null]

I haven't been able to trace how/why/where this null has been added, and although it doesn't really make sense for E4, it might make sense for other situations.  I think the code in EContentAdapter.setTarget(EObject) should have a guard within the loop:

  /**
   * Handles installation of the adapter on an EObject
   * by adding the adapter to each of the directly contained objects.
   */
  protected void setTarget(EObject target)
  {
    basicSetTarget(target);
    for (Iterator<? extends Notifier> i = resolve() ? 
           target.eContents().iterator() : 
           ((InternalEList<? extends Notifier>)target.eContents()).basicIterator();
         i.hasNext(); )
    {
      Notifier notifier = i.next();
      if(notifier != null) {
        addAdapter(notifier);
      }
    }
  }


Daemon Thread [Thread-1] (Suspended (exception NullPointerException))	
	UIEventPublisher(EContentAdapter).addAdapter(Notifier) line: 352	
	UIEventPublisher(EContentAdapter).setTarget(EObject) line: 225	
	UIEventPublisher(EContentAdapter).setTarget(Notifier) line: 188	
	MinimalEObjectImpl$1ArrayDelegatingAdapterList.didAdd(int, Adapter) line: 503	
	MinimalEObjectImpl$1ArrayDelegatingAdapterList.didAdd(int, Object) line: 1	
	MinimalEObjectImpl$1ArrayDelegatingAdapterList(ArrayDelegatingEList<E>).addUnique(E) line: 395	
	MinimalEObjectImpl$1ArrayDelegatingAdapterList(AbstractEList<E>).add(E) line: 307	
	E4Workbench.<init>(MApplicationElement, IEclipseContext) line: 72
Comment 1 Ed Merks CLA 2012-01-27 11:05:46 EST
Null in the list (a multi-valued feature's value) is most likely caused by multi-theaded updates to the that list. You'll find it's not possible to add null into such a list so it should not be possible for such values to come out.  The whole framework assumes such values can never be null, so patching one problem will just turn of the next, and the next and next, and in the end, a corrupted list is just not going to be fixed by such patching. Multi-threaded writes themselves need to be guarded by an appropriate threading policy.  Even adding an adapter needs to be done in a thread safe way; thread safety is not provided by the underlying data structures.