title: java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
body: <p>I am new to openCV and android. I am trying to load an image from Resources using bitmap and process the image using open CV. It gave me this error: </p>
  
  <pre><code>java.lang.NullPointerException: 
  Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
  </code></pre>
  
  <p><strong>Code:</strong>     </p>
  
  <pre><code>    Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.a1);
      ((ImageView)findViewById(R.id.imageView)).setImageBitmap(bm);
      Mat trainImage = new Mat(bm.getHeight(),bm.getWidth(),CvType.CV_32S);
      Utils.bitmapToMat(bm,trainImage);
      if(trainImage.empty()){
          Log.e(TAG,"Empty image");
      }
      else{
          Log.e(TAG,"Image Loaded");
      }
      Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);
  </code></pre>
  
  <p><strong>Here is the Exception message:</strong></p>
  
  <pre><code>E/SVM: Image Loaded
  D/AndroidRuntime: Shutting down VM
  E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.pragyan.facefeatures, PID: 3308
  java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1724)
    at com.example.pragyan.facefeatures.ExtractFeature.trainSvm(ExtractFeature.java:242)
    at com.example.pragyan.facefeatures.ExtractFeature.onOptionsItemSelected(ExtractFeature.java:313)
    at android.app.Activity.onMenuItemSelected(Activity.java:2885)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
    at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
    at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:663)
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
    at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
    at android.widget.AbsListView$3.run(AbsListView.java:3860)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>Can anyone tell me how to fix this. The image is loaded successfully and it still gives a <code>NullPointerException</code>.</p>