Search code examples
javaandroidandroid-camera2

Android Camera: Unboxing of 'characteristics.get(CameraCharacteristics.LENS_FACING)' may produce NPE


I'm implementing Camera2, and i'm getting the following warnings (I couldn't reproduce any of the "npe" that may occur) when I'm setting up the camera outputs (I get my code from a sample of google) the following line give me the following warning:

"Unboxing of 'characteristics.get(CameraCharacteristics.LENS_FACING)' may produce NPE."

 for (String cameraId : manager.getCameraIdList()) {
      CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

      if (characteristics.get(CameraCharacteristics.LENS_FACING) 
           == CameraCharacteristics.LENS_FACING_FRONT) { //THESE LINE IS WHERE THE WARNING APPEARS
           continue;
         }
   ....

Does someone knows how to inspect this in a proper way in order to catch this in case it happens?


Solution

  • you are comparing it with a primitive int, and characteristics.get(CameraCharacteristics.LENS_FACING) is probably returning an Integer which is an object. What happens is that you will have

    Integer integer = characteristics.get(CameraCharacteristics.LENS_FACING);
    if (integer.intValue() == CameraCharacteristics.LENS_FACING_FRONT) {
    
    }
    

    if integer is null, you will get a NPE accessing intValue()