-2

i have made an Qr-Scanner now the activity has button. if i click the button it opens the scanner so i'm doing good with it, but if i go back to the previous activity without clicking the button i get the NullPointerException from the onPause() .

@Override
public void onPause() {
    super.onPause();

    mScannerView.stopCamera();

}

if i go to previous activity without opening the camera then it is giving the error. someone help me

William Willi
  • 194
  • 1
  • 2
  • 19
  • This isn't Javascript. – Barmar Aug 31 '16 at 17:39
  • no i just wanted to know how to prevent the stopCamer() method if the startCamera() method is never used. @ 0X0nosugar – William Willi Aug 31 '16 at 17:50
  • 1
    Based on your comment, `if (mScannerView != null)` should fix your issue –  Aug 31 '16 at 17:56
  • @William Willi - I thought so. Pointing you to a duplicate is also meant to help you: the answers there do not deal with your code but they give examples of similar situations. Sooner or later you would have figured out that it's a good idea to check for (mScannerView != null) if they check for e.g. (foo == null) in the example code. – Bö macht Blau Aug 31 '16 at 18:00

1 Answers1

1

If you want to prevent stop() method from being called if the camera is not started, you must use a boolean expression to check.

inside onPause method:

if(mScannerView != null){
   mScannerView.stopCamera();
}
Mr_Anderson
  • 120
  • 2