Bug 226820 - NPE thrown from FrameworkConsole.getServices
Summary: NPE thrown from FrameworkConsole.getServices
Status: RESOLVED FIXED
Alias: None
Product: Equinox
Classification: Eclipse Project
Component: Framework (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4 M7   Edit
Assignee: equinox.framework-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2008-04-12 21:28 EDT by Simon Archer CLA
Modified: 2008-04-17 09:12 EDT (History)
1 user (show)

See Also:


Attachments
Simple patch that makes getServices() null tolerant. (908 bytes, patch)
2008-04-12 21:28 EDT, Simon Archer CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Archer CLA 2008-04-12 21:28:01 EDT
Created attachment 95819 [details]
Simple patch that makes getServices() null tolerant.

Today I saw an NPE thrown from the Equinox Console...

Exception in thread "OSGi Console" java.lang.NullPointerException
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.getServices(FrameworkConsole.java:368)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:297)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:285)
        at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:221)


This is probably rather rare, but is still possible.  I was using org.eclipse.osgi_3.3.2.R33x_v20080105.jar.

  public Object[] getServices() {
    ServiceReference[] serviceRefs = cptracker.getServiceReferences();
    Util.dsort(serviceRefs, 0, serviceRefs.length);  // Line 368
    ...
  }

The problem was that serviceRefs was null.  The ServiceTracker's getServiceReferences() method can return null.  The attached path is for the latest version of the code, and it simply returns null if serviceRefs is null:

  public Object[] getServices() {
    ServiceReference[] serviceRefs = cptracker.getServiceReferences();
    if (serviceRefs == null)
      return null;
    Util.dsort(serviceRefs, 0, serviceRefs.length);
    ...
  }
Comment 1 Thomas Watson CLA 2008-04-15 14:03:11 EDT
Thanks Simon.  I slightly modified the patch to return new Object[0].  If null was returned then we would just NPE later in FrameworkCommandInterpreter.docommand.
Comment 2 Simon Archer CLA 2008-04-15 14:35:33 EDT
Ah, I was just following the Javadoc, rather than checking for callers.  Please update the Javadoc for the FrameworkConsole method getServices(), which today claims that null can be returned.

* @return Array of service objects or <tt>null</tt> if no service
* are being tracked.

Thanks you Tom.
Comment 3 Thomas Watson CLA 2008-04-15 15:36:39 EDT
I updated the javadoc.  Thanks Simon.
Comment 4 Simon Archer CLA 2008-04-15 16:02:59 EDT
It's always a pleasure, Tom.  Thank you.
Comment 5 Thomas Watson CLA 2008-04-17 09:12:12 EDT
This got fixed for M7.