"AndroidJavaObject with null ptr" after purchasing a product

Hi folks!

I’m trying to use the IAP Package without the Unity.Services.Core dependency nor Analytics.
But I’m getting the following error every time I finish a purchase:

2024/05/08 12:25:33.514 25155 25685 Error Unity Exception: JNI: Init'd AndroidJavaObject with null ptr!
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine.AndroidJavaObject..ctor (System.IntPtr jobject) [0x0001d] in /home/bokken/build/output/unity/unity/Modules/AndroidJNI/AndroidJava.cs:344
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine._AndroidJNIHelper.GetSignature (System.Object obj) [0x001b6] in /home/bokken/build/output/unity/unity/Modules/AndroidJNI/AndroidJava.cs:1740
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine._AndroidJNIHelper.GetSignature (System.Object[] args) [0x00039] in /home/bokken/build/output/unity/unity/Modules/AndroidJNI/AndroidJava.cs:1791
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine._AndroidJNIHelper.GetMethodID (System.IntPtr jclass, System.String methodName, System.Object[] args, System.Boolean isStatic) [0x00004] in /home/bokken/build/output/unity/unity/Modules/AndroidJNI/AndroidJava.cs:1598
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine.AndroidJNIHelper.GetMethodID (System.IntPtr jclass, System.String methodName, System.Object[] args, System.Boolean isStatic) [0x00005] in /home/bokken/build/output/unity/unity/Modules/AndroidJNI/AndroidJNI.bindings.cs:166
2024/05/08 12:25:33.514 25155 25685 Error Unity   at UnityEngine.

Have you faced this issue before?

We are having the exact problem with a third party SDK, when using its API in an Async Task. Did you find out the cause of the error and its solution?

I have exactly the same problem when trying to execute a command using AndroidJavaObject in a separate Thread.

Hello, would you be able to provide more information to help us investigate this?
We would need the version of IAP, the Unity editor version and some more information on the AndroidJavaObject (callstack and/or code being called leading to the error).

1 Like

Hello.
I’m trying to program an AT-10A tablet using Unity for this. The Unity version is 2022.3.21f1.

The tablet manufacturer provided me with a file “canbus_api.aar”, which contains the CanBus communication functions that I need to access.

I was able to communicate with their API successfully, using the code below:

using UnityEngine;

public class JavaClassCAN : MonoBehaviour {

    AndroidJavaObject canBusHelper;

    private void Start() {
        canBusHelper = new AndroidJavaObject("com.android.canbus.CanBusHelper");

        //config serial baudrate
        var retConfigSerial = canBusHelper.Call<int>("setSerialBaudrate", 1, 115200, 8, 0, 1); //OK - it works
        if (retConfigSerial < 0) {
            Debug.Log("ERROR - Config Serial Baudrate");
        }
        Debug.Log("Return 'setSerialBaudrate': " + retConfigSerial.ToString());

        //initialize canbus
        var retInitCAN = canBusHelper.Call<int>("initialize", 1, 115200, 500000, false); //OK - it works
        if (retInitCAN < 0) {
            Debug.Log("ERROR - Config CAN");
        }
        Debug.Log("Return 'initialize': " + retInitCAN.ToString());

        //Start read CanBus
        if (canBusHelper != null) {
            var retReadCan = canBusHelper.Call<int>("readCan", 1, new CanBusCallbackProxy()); //OK - it works
            Debug.Log(retReadCan);
        }
        else {
            Debug.LogError("AndroidJavaObject canBusHelper is null");
        }
    }

    public class CanBusCallbackProxy : AndroidJavaProxy {
        public CanBusCallbackProxy() : base("com.android.canbus.CanBusHelper$CanBusCallback") {
        }
        public void onSetError() {
            Debug.Log("zyz0 --> onSetError");
        }
        public void onSendError() {
            Debug.Log("zyz0 --> onSendError");
        }
        public void onIdError(int count) {
            Debug.Log("onIdError: " + count);
        }
        public void onReceiveCanbusData(int FF, int RTR, int DLC, int ID, int[] DATA) {
            Debug.Log("onReceiveCanbusData: FF=" + FF + ", RTR=" + RTR + ", DLC=" + DLC + ", ID=" + ID.ToString("X"));
            string dataString = "DATA: ";
            for (int i = 0; i < DATA.Length; i++) {
                dataString += DATA[i].ToString("X") + " ";
            }
            Debug.Log(dataString);
        }
    }
}

It’s quite simple and it works, as shown in logcat:

However, the “readCan” function is blocking, leaving the program stuck inside a while while waiting to receive data, and this obviously blocks the main unity thread and blocks the execution of the program itself.

The solution to this is apparently simple, just move the “readCan” function to a separate thread and everything will work fine. So I created this code below, making the function initialize in a new Thread.

using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class JavaClassCAN : MonoBehaviour {

    AndroidJavaObject canBusHelper;

    private Thread canReadThread;
    private bool runThread;

    private void Start() {
        canBusHelper = new AndroidJavaObject("com.android.canbus.CanBusHelper");

        //config serial baudrate
        var retConfigSerial = canBusHelper.Call<int>("setSerialBaudrate", 1, 115200, 8, 0, 1);
        if (retConfigSerial < 0) {
            Debug.Log("ERROR - Config Serial Baudrate");
        }
        Debug.Log("Return 'setSerialBaudrate': " + retConfigSerial.ToString());

        //initialize canbus
        var retInitCAN = canBusHelper.Call<int>("initialize", 1, 115200, 500000, false);
        if (retInitCAN < 0) {
            Debug.Log("ERROR - Config CAN");
        }
        Debug.Log("Return 'initialize': " + retInitCAN.ToString());

        //Start thread
        runThread = true;
        canReadThread = new Thread(ReadCanData);
        canReadThread.Start();
    }


    private void OnDisable() {
        runThread = false;
        if (canReadThread != null && canReadThread.IsAlive) {
            canReadThread.Join();
        }
    }

    private void ReadCanData() {
        Thread.Sleep(1000);
        if (runThread) {
            if (canBusHelper != null) {
                var retReadCan = canBusHelper.Call<int>("readCan", 1, new CanBusCallbackProxy()); //HERE - BUG
                Debug.Log(retReadCan);
            }
            else {
                Debug.LogError("AndroidJavaObject canBusHelper is null");
            }
        }
    }

    public class CanBusCallbackProxy : AndroidJavaProxy {
        public CanBusCallbackProxy() : base("com.android.canbus.CanBusHelper$CanBusCallback") {
        }
        public void onSetError() {
            Debug.Log("zyz0 --> onSetError");
        }
        public void onSendError() {
            Debug.Log("zyz0 --> onSendError");
        }
        public void onIdError(int count) {
            Debug.Log("onIdError: " + count);
        }
        public void onReceiveCanbusData(int FF, int RTR, int DLC, int ID, int[] DATA) {
            Debug.Log("onReceiveCanbusData: FF=" + FF + ", RTR=" + RTR + ", DLC=" + DLC + ", ID=" + ID.ToString("X"));
            string dataString = "DATA: ";
            for (int i = 0; i < DATA.Length; i++) {
                dataString += DATA[i].ToString("X") + " ";
            }
            Debug.Log(dataString);
        }
    }
}

However, this time, when trying to run the readCan function, I get the error “Error Unity Exception: JNI: Init’d AndroidJavaObject with null ptr!”


I see you made another post in the Android section and already got an answer:

I would encourage you to follow up with them there if it isn’t related to In-App Purchasing since they’ll be able to provide better support.

1 Like

Did you find a solution to this issue? I’m seeing it too when making purchases on Android using Unity IAP 4.11 and Unity 2022.3.29

Same here, Unity IAP 4.12.0 has been error.

2024-06-12 10:50:16.738 1994-2026 VMSG system_server I processRequest:104 called for hcal
2024-06-12 10:50:16.741 13754-18363 FA com.android.vending W Failed to retrieve Firebase Instance Id
2024-06-12 10:50:16.757 2265-2265 BstCommand…or-Service com.bluestacks.BstCommandProcessor D onStartCommand called
2024-06-12 10:50:16.757 18159-18159 ProxyBillingActivity xxxxxx W Activity finished with resultCode 0 and billing’s responseCode: 1
2024-06-12 10:50:16.759 1994-3968 InputMetho…gerService system_server D packageName=xxxxx, activityName=com.android.billingclient.api.ProxyBillingActivity
2024-06-12 10:50:16.759 1994-3968 InputMetho…gerService system_server D ime_enabled = false is same as last value, no change
2024-06-12 10:50:16.760 18159-18159 BillingLogger xxxxxx W Unable to log.
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at com.google.android.gms.internal.play_billing.zzbp.(com.android.billingclient:billing@@6.2.1:4)
at com.google.android.gms.internal.play_billing.zzdd.zzA(com.android.billingclient:billing@@6.2.1:4)
at com.google.android.gms.internal.play_billing.zzdd.zzk(com.android.billingclient:billing@@6.2.1:2)
at com.google.android.gms.internal.play_billing.zzgy.zzB(com.android.billingclient:billing@@6.2.1:1)
at com.android.billingclient.api.zzcd.zzc(com.android.billingclient:billing@@6.2.1:1)
at com.android.billingclient.api.zzj.onReceive(com.android.billingclient:billing@@6.2.1:12)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2024-06-12 10:50:16.760 18159-18159 BillingLogger xxxxxxx W Unable to create logging payload
java.lang.NullPointerException: Attempt to invoke interface method ‘java.util.Iterator java.util.List.iterator()’ on a null object reference
at com.android.billingclient.api.zzcd.zzf(com.android.billingclient:billing@@6.2.1:7)
at com.android.billingclient.api.zzj.onReceive(com.android.billingclient:billing@@6.2.1:20)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2024-06-12 10:50:16.764 1994-11093 ActivityManager system_server D TopActivityInfo, pkgName: xxxxx activityName: xxxxxx/com.google.firebase.MessagingUnityPlayerActivity callingPackage: bstSpecialAppKeyboardHandlingEnabled = false

@Yannick_D do you have any information on this issue?

Back here pinging @Yannick_D as it has been a few months of no activity on this thread. Still experiencing this issue when making purchases on Android.