This is getting me crazy already, why am I getting a NullReference on coapClient in the CoapManager script whenever I click the button I have in my scene to call on the LightOn() in my LedManager script. I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
CoapManager.GetUri (System.String ip, System.String resource) (at Assets/Scripts/CoapManager.cs:71)
LedManager.ChangeLedState (System.String state) (at Assets/Scripts/LedManager.cs:55)
LedManager.LightOnButton () (at Assets/Scripts/LedManager.cs:39)
CoapManager:
using System;
using UnityEngine;
public class ResponseReceivedEventArgs : EventArgs
{
public string Resource { get; set; }
public string Data { get; set; }
}
public class CoapManager : MonoBehaviour
{
public event EventHandler<ResponseReceivedEventArgs> ResponseReceivedHandler;
private AndroidJavaObject coapClient;
void Start()
{
try
{
coapClient = new AndroidJavaObject("icarus.edu.californiumunitylibrary.CoapClientManager");
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
}
public string GetUri(string ip, string resource)
{
string res = coapClient.Call<string>("getUri", ip, resource);
return res;
}
public void DoPut(string uri, string data)
{
coapClient.Call("doPut", uri, data);
}
public void DoGet(string uri)
{
coapClient.Call("doGet", uri);
}
public void GetResponse(string response)
{
ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
..........}
LedManager:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LedManager : MonoBehaviour
{
[SerializeField]
private CoapManager coapManager;
[SerializeField]
// private Text label;
private dropDown sel_class;
public string ip_add;
private eventSensors eventSensors;
void Start()
{
ip_add = sel_class.ip_add;
coapManager.ResponseReceivedHandler += ResponseReceived;
}
void update(){
}
public void LightOnButton() //1=ON
{
print("in LightON");
//DisableAllButtons();
ChangeLedState("1");
}
public void LedOffButton() //0=OFF
{
print("in LightOFF");
ChangeLedState("0");
}
private void ChangeLedState(string state)
{
//uri is the returned petition url generated by
//public string GetUri(string ip, string resource) in the CoapManager.cs
string uri = coapManager.GetUri("192.168.1.120", "led");
//DpPut to modifies the value of the state
coapManager.DoPut(uri, state);
}
}
Could any one please help out?