简体   繁体   English

java.lang.NullPointerException:尝试从空对象引用中的字段“ long org.opencv.core.Mat.nativeObj”读取

[英]java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

I am new to openCV and android. 我是openCV和android的新手。 I am trying to load an image from Resources using bitmap and process the image using open CV. 我试图使用位图从资源中加载图像,并使用打开的CV处理图像。 It gave me this error: 它给了我这个错误:

java.lang.NullPointerException: 
Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

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);

Here is the Exception message: 这是异常消息:

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)

Can anyone tell me how to fix this. 谁能告诉我如何解决此问题。 The image is loaded successfully and it still gives a NullPointerException . 图像已成功加载,并且仍然提供NullPointerException

I think you've not initialized the Mat trainGray . 我认为您尚未初始化Mat trainGray Try the following. 尝试以下方法。

Mat trainGray = new Mat();
Imgproc.cvtColor(trainImage, trainGray, Imgproc.COLOR_RGBA2GRAY);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.PersonalShopper.ps_name' on a null object reference - java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.PersonalShopper.ps_name' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.io.InputStream.read(byte[])” - java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference java.lang.NullPointerException:尝试在空对象引用上写入字段 android.widget.TextView $ViewHolder.numberTextView - java.lang.NullPointerException: Attempt to write to field android.widget.TextView $ViewHolder.numberTextView on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法,而它不为空 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int org.json.JSONObject.getInt(java.lang.String)' - java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference java.lang.NullPointerException:尝试在从服务器到微调器的空对象引用上调用接口方法“int java.util.List.size()” - java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference from server to spinner java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法… - java.lang.NullPointerException: Attempt to invoke virtual method … on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference 引发者:java.lang.NullPointerException:尝试在null对象引用上调用接口方法 - Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法'ActionBar.setNavigationMode(int)' - java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM