android : camera.getParameters() crashes app

Anonymous picture Anonymous · Dec 15, 2013 · Viewed 8k times · Source

i'm using this code to use the camera LED as flashlight but it crashes when the service starts. Using Log.w() statements i found it stops at param = camera.getParameters();. i have included the android.permission.CAMERA in my manifest. i don't understand what causes the crash. how can i fix this?

    public class Flashlight extends Service {

    private Camera camera;
    private Parameters param;
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        Flashlight getService() {
            return Flashlight.this;
        }
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
         Log.w("1","camera.open");
         Camera.open();
         Log.w("1","camera.getParameters");
         param = camera.getParameters();
         Log.w("1","i got camera parameters");
         List<String> pList = camera.getParameters().getSupportedFlashModes();
         if(pList.contains(Parameters.FLASH_MODE_TORCH)){
             param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
         }
         else if(pList.contains(Parameters.FLASH_MODE_ON)){
             param.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
         }
         param.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
         try{
             camera.setParameters(param);
             camera.startPreview();
         }
         catch (Exception e){
             Toast.makeText(getApplicationContext(), "Your device does not have flash light support", Toast.LENGTH_SHORT).show();

         }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Parameters p=camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        camera.release();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

this is the LogCcat

12-15 15:44:32.037: W/1(26304): camera.open
12-15 15:44:32.347: W/1(26304): camera.getParameters
12-15 15:44:32.347: D/AndroidRuntime(26304): Shutting down VM
12-15 15:44:32.347: W/dalvikvm(26304): threadid=1: thread exiting with uncaught exception (group=0x40a9e228)
12-15 15:44:32.357: E/AndroidRuntime(26304): FATAL EXCEPTION: main
12-15 15:44:32.357: E/AndroidRuntime(26304): java.lang.RuntimeException: Unable to start service com.bill.deuterh.Flashlight@40dd0978 with Intent { cmp=com.bill.deuterh/.Flashlight }: java.lang.NullPointerException
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2738)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.access$1900(ActivityThread.java:139)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.os.Looper.loop(Looper.java:154)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.main(ActivityThread.java:4945)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at java.lang.reflect.Method.invoke(Method.java:511)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at dalvik.system.NativeStart.main(Native Method)
12-15 15:44:32.357: E/AndroidRuntime(26304): Caused by: java.lang.NullPointerException
12-15 15:44:32.357: E/AndroidRuntime(26304):    at com.bill.deuterh.Flashlight.onStartCommand(Flashlight.java:32)
12-15 15:44:32.357: E/AndroidRuntime(26304):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2715)
12-15 15:44:32.357: E/AndroidRuntime(26304):    ... 10 more

i finally figured it out after a lot of trial end error.i'm posting the correct code for getting the parameteres, for other users with this problem

     camera = Camera.open();
     Camera.Parameters param = camera.getParameters();

Answer

Josh picture Josh · Sep 5, 2014

Add a condition to check if the object is null first:

camera = Camera.open();
if (camera.getParameters()!=null) {
    Camera.Parameters param = camera.getParameters();
}