Null Reference when calling AndroidJavaObject class with return value

Hello. I am trying to learn how to create an android plugin for a game. I am getting this error in LogCat: ```
2022-03-19 13:52:03.157 7866-7911/? E/Unity: Error: Object reference not set to an instance of an object :: at BlueTootheLEWrapperAndroid.Tester (System.String name) [0x0000a] in <3c57ec3dcf8d4354bd414392ea186135>:0
at TestUiManipulator.ChangeTextField () [0x00000] in <3c57ec3dcf8d4354bd414392ea186135>:0


In my scene I have a button that calls TestUimanipulator.cs > ChangeTextField() and then that calls BluetoothLEWrapperAndroid.cs > Tester(string). I have confirmed that name does have a value, it is not null. There is no error logged during the instantiation of the wrapper.

Here is the code:

```csharp
//TestUiManipulator

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestUiManipulator : MonoBehaviour
{
    public Text TextField;


    private IBlueToothLE BlueTooth;

    public void Start()
    {
        if (Application.platform == RuntimePlatform.Android)
            BlueTooth = new BlueTootheLEWrapperAndroid();
        else
            BlueTooth = new BlueToothWrapperWindows();
          
    }
    public void ChangeTextField()
    {
        try
        {
            TextField.text = BlueTooth.Tester("Jeremy");//"OMG! WHY DID YOU CLICK THE BUTON!";
        }
        catch (Exception e)
        {
            TextField.text = "Error: " +  e.Message;
            Debug.LogError($"Error: {e.Message} :: {e.StackTrace}");
        }
    }
}
//BluetoothLEWrapperAndroid.cs

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

public class BlueTootheLEWrapperAndroid : MonoBehaviour, IBlueToothLE
{
    private AndroidJavaObject BlueToothLE;

    // Start is called before the first frame update
    void Start()
    {
        try
        {
            BlueToothLE = new AndroidJavaObject("com.studiokitsune.bluetoothlelibrary.BluetoothLeRunner");
        }
        catch(Exception e)
        {
            Debug.LogError($"Error instantiating BlueToothLERunner :: {e.Message} :: {e.StackTrace}");
        }
    }

    // Update is called once per frame
    void Update()
    {
      
    }
  
    public string Tester(string name)
    {
        if (name == null)
            name = "No Name";
        return BlueToothLE.Call<string>("tester", new object[] { name });

    }
}

Any help would be appreciated, thank you.

EDIT: The android Code

//Java was not an option for code insert but this is Java

package com.studiokitsune.bluetoothlelibrary;

import android.util.Log;

public class BluetoothLeRunner {
    public String tester(String name)
    {
        //Log.d("Unity", "Hello from Android plugin");
        return "Hello from android " + name;
    }
}

Your “BlueTootheLEWrapperAndroid” class is a MonoBehaviour (since you derived it from MonoBehaviour). You can not create a MonoBehaviour with “new” as MonoBehaviours are components which can only live on gameobjects. You can only create components by either attaching them in the Unity editor to a gameobject, or through code by using AddComponent<BlueTootheLEWrapperAndroid>();

Apart from that there could be countless of other things be wrong when you used the JNI bridge. Maybe your java class was not created properly. Maybe you have the signature of your “tester” method wrong, the list is endless. We don’t know how your java class looks like, so it’s hard to tell what may be wrong.

1 Like

Sorry I was actually in the process of editing the post when you posted. The Java class in now on the original post. It definitely could have some type of issue, my background is in c#/Blazor , this was my first attempt at anything Java and I don’t know too much about unity. I will try changing the instantiation method.

Although I will say the BluetoothWrapperWindows() worked so I don’t think it will have anything to do with the instatiation method

Thanks for the help Bunny, you were right about Monobehavior, I got rid of it in my wrapper, got rid of start and update and added a constructor and now it works!! I am so excited to start this, thank you again for the help!