title: Try to invoke virtual method on a null object reference
body: <p>I am trying to make a barcode scanner app, and I want to add scan results to a certain list. In the MainActivity, I have a certain button that should send me to MyList activity, but then the app crashes and I don't know how to solve it.</p>
  
  <p>So here is my code:</p>
  
  <pre><code>public class MainActivity extends AbsRuntimePermission implements ZXingScannerView.ResultHandler{
  
  private ZXingScannerView zXingScannerView;
  private static final int REQUEST_PERMISSION = 10;
  boolean ok = false, chk = false;
  private String scanResult;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      chk = getIntent().getBooleanExtra("check", chk);
  
      if(chk) {
          chk = false;
  
          ok = true;
          zXingScannerView = new ZXingScannerView(getApplicationContext());
          setContentView(zXingScannerView);
          zXingScannerView.setResultHandler(this);
          zXingScannerView.startCamera();
      }
  }
  
  public void scan(View view){
      requestAppPermissions(new String[]{Manifest.permission.CAMERA}, R.string.msg, REQUEST_PERMISSION);
      ok = true;
      zXingScannerView = new ZXingScannerView(getApplicationContext());
      setContentView(zXingScannerView);
      zXingScannerView.setResultHandler(this);
      zXingScannerView.startCamera();
  }
  
  @Override
  public void onPermissionGranted(int requestCode) {
      if(ok)
          Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_LONG).show();
  }
  
  @Override
  public void onResume(){
      super.onResume();
  
      if(ok)
      {
          if(zXingScannerView == null)
          {
              zXingScannerView = new ZXingScannerView(this);
              setContentView(zXingScannerView);
          }
          zXingScannerView.setResultHandler(this);
          zXingScannerView.startCamera();
      }
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      zXingScannerView.stopCamera();
  }
  
  @Override
  public void handleResult(final Result result) {
      scanResult = result.getText();
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Result");
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              zXingScannerView.resumeCameraPreview(MainActivity.this);
          }
      });
      builder.setNeutralButton("GO", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              try
              {
                  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(scanResult));
                  startActivity(intent);
              }catch (Exception ex)
              {
                  Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                  intent.putExtra(SearchManager.QUERY, scanResult);
                  startActivity(intent);
              }
          }
      });
  
      builder.setNegativeButton("Add to list", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              Intent intent = new Intent(MainActivity.this, MyList.class);
              intent.putExtra("CODE", scanResult);
              startActivity(intent);
          }
      });
  
      builder.setMessage(scanResult);
      AlertDialog alert = builder.create();
      alert.show();
  }
  
  public void Lista(View view){
      Intent iNtent = new Intent(MainActivity.this, MyList.class);
      startActivity(iNtent);
  }
  
  }` 
  </code></pre>
  
  <p>And this is my error report:</p>
  
  <pre><code>01-12 09:42:26.013 2670-2670/com.example.tchibo.justqr E/AndroidRuntime:        FATAL EXCEPTION: main
  
  Process: com.example.tchibo.justqr, PID: 2670
  
  java.lang.RuntimeException: Unable to pause activity {com.example.tchibo.justqr/com.example.tchibo.justqr.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference                                                                          at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:3976)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3942)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3916)
  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3890)
  at android.app.ActivityThread.-wrap15(Unknown Source:0)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
  at android.os.Handler.dispatchMessage(Handler.java:105)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6541)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()' on a null object reference
  </code></pre>
  
  <p>Actually, it says that is trying to invoke virtual method <code>void me.dm7.barcodescanner.zxing.ZXingScannerView.stopCamera()</code> on a <code>null</code> object reference.</p>
  
  <p>(sorry for the post style but i'm not familiar with it)</p>