0

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

}

CoapManager

LedManager

Could any one please help out?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
az6bcn
  • 339
  • 3
  • 19
  • If you google for the error message you'll find a lot of explanations that boil down to - you are trying to use a value that is null. Given your code, this is easy to happen if the private method `Start()` doesn't get called before `GetUri()`. Who's calling `Start()`? The code you posted doesn't. Why aren't you using a constructor? – Panagiotis Kanavos Mar 13 '17 at 10:12
  • 1
    @KrisVandermotten maybe because it was never called? `Start()` is a private method. And this *is* a duplicate. To find the code responsible for the null, one has to follow the steps in `What is a NullReferenceException, and how do I fix it?`. – Panagiotis Kanavos Mar 13 '17 at 10:14
  • @KrisVandermotten It returns null because it's not standard java lib. Add this to the project and then try to call it... Another thing is that the error is inside `GetUri` so he provided null argument or in fact `AndroidJavaObject` contains null reference. – mrogal.ski Mar 13 '17 at 10:14
  • @m.rogalski what does Java have to do with a C# question? Anyway, the `new` operator can't return `null`. Either it returns an object or it throws – Panagiotis Kanavos Mar 13 '17 at 10:15
  • @PanagiotisKanavos It's **UNITY** read about that first. `Start` method is called by the engine and yes you can import `Java` libraries as plugins that will be attached during build on Android platform ... – mrogal.ski Mar 13 '17 at 10:22
  • @m.rogalski it's C# even if the framework is Unity. `new` can't return a null, no matter what libraries are involved – Panagiotis Kanavos Mar 13 '17 at 10:40
  • @PanagiotisKanavos `AndroidJavaObject` is just a wrapper around android java object so yes it can return null in the same sense like `Nullable<>` can. Even though you can do something like `new Nullable()` and it will return instance of the `Nullable` but still wrapped element is `null`... – mrogal.ski Mar 13 '17 at 10:43
  • @m.rogalski no it can't and Nullable is a class, not a method. The thing that creates instances is `new`, a part of the language itself, which is guaranteed to either return a full object or throw. It will never return null or a partially constructed object. It doesn't matter that `AndroidJavaObject` is a wrapper. If the constructor throws, you get nothing back except an exception. BTW If you create a `Nullable` from a Null, you get back a class whose `.Value` is a null, not a null that can throw. – Panagiotis Kanavos Mar 13 '17 at 10:46
  • Either `Start()` is never called, or the constructor throws (thus leaving `coapClient` null) but the `catch` statement hides the error and the OP never checked the log – Panagiotis Kanavos Mar 13 '17 at 10:48
  • 1
    `Start` is an internal Unity method. It's called automatically when object is created within engine. _"Either Start() is never called"_ This sentence just showed how wrong you're. It seams for me that you don't even know what you're talking about. – mrogal.ski Mar 13 '17 at 10:52
  • The Start() is called when Unity starts running.. The error is in the GetUri() but I just don't understand why the coapClient which is an AndroidJavaObject instance is null. I have been reading about the error but it happens when you are referring to any object instead of it's instance... but in my case I am referring to the coapClient which is an instance of AndroidJavaObject. – az6bcn Mar 13 '17 at 11:51
  • @OlusegunOdumosu it looks like Unity *does* break the language and you [can't use `new` at all](http://stackoverflow.com/questions/37398538/unity-null-while-making-new-class-instance) inside a class that inherits from MonoBehaviour. As the [linked answer shows](http://stackoverflow.com/a/37399263/134204) you have to use `AddComponent` or `Instantiate` – Panagiotis Kanavos Mar 13 '17 at 15:36
  • @m.rogalski that makes two of us after all - you can't use `new` at all inside a class that inherits from `MonoBehaviour`. The language *is* broken but you don't get any warnings – Panagiotis Kanavos Mar 13 '17 at 15:37
  • Possible duplicate of [Unity: Null while making new class instance](http://stackoverflow.com/questions/37398538/unity-null-while-making-new-class-instance) – Panagiotis Kanavos Mar 13 '17 at 15:39
  • @OlusegunOdumosu Did you put java library inside of `Assets\Plugins\Android` folder ? – mrogal.ski Mar 13 '17 at 15:56
  • yes... i imported the plugins. It worked once and just stopped working all of a sudden throwing up that errow. Never changed anything in the code – az6bcn Mar 13 '17 at 16:09
  • Are you debugging this on real device or inside unity ? – mrogal.ski Mar 13 '17 at 17:00
  • On a real device.. I could see it in the Android sdk monitor – az6bcn Mar 13 '17 at 17:05
  • Maybe try with `AndroidJavaClass` instead of `AndroidJavaObject`. Should not be much of a difference but ... it's Unity and you never know :D – mrogal.ski Mar 14 '17 at 08:59
  • It's working now... I noticed the start() was printing out whatever it's in it twice. Then I pressed the Collapse Button in the console and all is fine. The code didn't have any problem. – az6bcn Mar 16 '17 at 22:25

1 Answers1

0

There was nothing wrong with the Logic, the problem was solved by just enabling the COLLAPSE tab when I executed the program. I don't really know why tho, since I had it enabled I have not got the error again.

az6bcn
  • 339
  • 3
  • 19