title: NullPointerException from UsbManager
body: <p>I have some code which looks like</p>
  
  <pre><code>UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
  usbManager.getDeviceList();
  </code></pre>
  
  <p>When I use an Android 5.0 emulator, I get this crash:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke interface method 'void android.hardware.usb.IUsbManager.getDeviceList(android.os.Bundle)' on a null object reference
      at android.hardware.usb.UsbManager.getDeviceList(UsbManager.java:243)
      ...
  </code></pre>
  
  <p>I understand that the official Android emulator has no USB support, but I would expect to see an empty list of USB devices rather than a crash.</p>
  
  <p>I peaked in the Android code a bit, and <code>ServiceManager</code>'s map of services didn't have an entry for <code>"usb"</code>. Nothing in the Android 21 stack seems to handle the null value returned by <code>ServiceManager</code>.</p>
  
  <p>The code works fine on emulated 4.4 devices, but crashes on 5.0 devices. CPU architecture doesn't seem to make a difference; I tried ARM and x86.</p>
  
  <p>It also works fine on all Genymotion devices I've tried, but I need to get this working on CentOS CI hosts, and getting Genymotion to run on CentOS seems to require intrusive changes.</p>
  
  <p>Any ideas for a fix or workaround? As a last resort I could catch <code>UsbManager</code>'s NPE, but it would be pretty bad as I also use a third party library which interacts with <code>UsbManager</code>.</p>