question

vanKraster-4073 avatar image
0 Votes"
vanKraster-4073 asked YonglunLiu-MSFT commented

Call method from aar file give error when cast Generic.Ilist<string> to Java.Ilist<string>

Hi to all,
As mentioned I have successfully imported and used some methods from aar file in my xamarin project, but ONE METHOD gives me error.

The method signature is this:

 // Metadata.xml XPath method reference: path="/api/package[@name='com.consumerphysics.android.sdk.sciosdk']/class[@name='ScioCloud']/method[@name='analyze' and count(parameter)=3 and parameter[1][@type='com.consumerphysics.android.sdk.model.ScioReading'] and parameter[2][@type='java.util.List&lt;java.lang.String&gt;'] and parameter[3][@type='com.consumerphysics.android.sdk.callback.cloud.ScioCloudAnalyzeManyCallback']]"
 [Register ("analyze", "(Lcom/consumerphysics/android/sdk/model/ScioReading;Ljava/util/List;Lcom/consumerphysics/android/sdk/callback/cloud/ScioCloudAnalyzeManyCallback;)V", "GetAnalyze_Lcom_consumerphysics_android_sdk_model_ScioReading_Ljava_util_List_Lcom_consumerphysics_android_sdk_callback_cloud_ScioCloudAnalyzeManyCallback_Handler")]
 public virtual unsafe void Analyze (global::Com.Consumerphysics.Android.Sdk.Model.ScioReading p0, global::System.Collections.Generic.IList<string> p1, global::Com.Consumerphysics.Android.Sdk.Callback.Cloud.IScioCloudAnalyzeManyCallback p2)
 {
 const string __id = "analyze.(Lcom/consumerphysics/android/sdk/model/ScioReading;Ljava/util/List;Lcom/consumerphysics/android/sdk/callback/cloud/ScioCloudAnalyzeManyCallback;)V";
 IntPtr native_p1 = global::Android.Runtime.JavaList<string>.ToLocalJniHandle (p1);
 try {
 JniArgumentValue* __args = stackalloc JniArgumentValue [3];
 __args [0] = new JniArgumentValue ((p0 == null) ? IntPtr.Zero : ((global::Java.Lang.Object) p0).Handle);
 __args [1] = new JniArgumentValue (native_p1);
 __args [2] = new JniArgumentValue ((p2 == null) ? IntPtr.Zero : ((global::Java.Lang.Object) p2).Handle);
 _members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
 } finally {
 JNIEnv.DeleteLocalRef (native_p1);
 global::System.GC.KeepAlive (p0);
 global::System.GC.KeepAlive (p1);
 global::System.GC.KeepAlive (p2);
 }
 }

As You can see it translates the list to java list here: IntPtr native_p1 = global::Android.Runtime.JavaList<string>.ToLocalJniHandle (p1);
The method is 100% functionally in Java, only in xamarin it gives the error bellow.

When I call the method I have this error

“{Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <1921523bc22e407fa9a0855bdae29335>:0
at Android.Runtime.JNIEnv.CallVoidMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNIEnv.g.cs:264
at Com.Consumerphysics.Android.Sdk.Sciosdk.ScioCloud.Analyze (Com.Consumerphysics.Android.Sdk.Model.ScioReading p0, System.Collections.Generic.IList`1[T] p1, Com.Consumerphysics.Android.Sdk.Callback.Cloud.IScioCloudAnalyzeManyCallback p2) [0x00085] in C:\Users\dmaruggi\Mobile\FTS21_library_simple app\ScioLibrary\SCiOLibrary\AARSCiO\obj\Debug\generated\src\Com.Consumerphysics.Android.Sdk.Sciosdk.ScioCloud.cs:257
at SCiOLibrary.Android.Cloud.ScioCloudAndroid.AnalyzeModels (System.Collections.Generic.IList`1[T] modelIdsToAnalyze) [0x00057] in C:\Users\dmaruggi\Mobile\FTS21_library_simple app\ScioLibrary\SCiOLibrary\SCiOLibrary\Android\Cloud\ScioCloudAndroid.android.cs:68
--- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at com.consumerphysics.android.sdk.sciosdk.ScioInternalCloud.prepareAnalyzeJson(ScioInternalCloud.java:951)
at com.consumerphysics.android.sdk.sciosdk.ScioInternalCloud.analyze(ScioInternalCloud.java:690)
at com.consumerphysics.android.sdk.sciosdk.ScioInternalCloud.analyze(ScioInternalCloud.java:574)
at com.consumerphysics.android.sdk.sciosdk.ScioCloud.analyze(ScioCloud.java:218)
at com.consumerphysics.android.sdk.sciosdk.ScioCloud.analyze(ScioCloud.java:199)
at crc64f81d35e176148202.ScioScanHandlerBool.n_onSuccess(Native Method)
at crc64f81d35e176148202.ScioScanHandlerBool.onSuccess(ScioScanHandlerBool.java:48)
at com.consumerphysics.android.sdk.sciosdk.ScioInternalDevice$8$2.run(ScioInternalDevice.java:1756)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
}”

dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference.

The exception indicates that you are trying to dereference something that is null.

Have you checked the error line that the stack trace mentioned?

I suggest that when you are about to create the JSON object to pass into prepareAnalyzeJson, you should print out the string or log it to a file, so you can see what is missing.

0 Votes 0 ·

Hi, I cannot edit the aar file cause is not mine. But I assure you I send all parameters not null... I am quite sure that is an error while converting from C# list to Java List

0 Votes 0 ·

As far as the information is currently available, we cannot reproduce this issue.

If you could make sure that the issue is caused by converting from C# list to Java List, you could refer to Report a problem with the Visual Studio product or installer to report the problem to our tech team.

0 Votes 0 ·

0 Answers