0

I am creating an ASP.NET web form for viewing real-time data across multiple web-compliant devices. The real-time database I have selected is Firebase and am using the Firesharp SDK to interface with the data. However, when I try inserting the data into the database, I receive the following error: System.NullReferenceException:

'Object reference not set to an instance of an object.'
FireData result = response.ResultAs<FireData>(); //on this line. 

I adhered to the following documentation when implementing the connection to the database https://libraries.io/nuget/FireSharp but I am not sure why I might be getting a null object reference error in this context having closely replicated the code in said documentation.

Here is my class structure:

public class FirebaseConnection
    {

        IFirebaseConfig config;
        IFirebaseClient client;
        FirebaseResponse firebaseResponse;
        FireData data;

        public FirebaseConnection(string secret, string path)
        {
            config = new FirebaseConfig {
                AuthSecret = secret,
                BasePath = path
            };

            client = new FireSharp.FirebaseClient(config);
        }


        public bool ConnectionStatus()
        {
            return client != null;
        }

        public IFirebaseConfig Config()
        {
            return this.config;
        }

        public IFirebaseClient Client()
        {
            return this.client; 
        }

        public async void WriteStream(FireData insertData)
        {
            SetResponse response = await client.SetTaskAsync("bluezonedemo/", insertData);
            FireData result = response.ResultAs<FireData>();
            Debug.WriteLine("Method has been called."); 
        }

        public async void ReadStream()
        {
            firebaseResponse = await client.GetTaskAsync("Info");
            data = firebaseResponse.ResultAs<FireData>();
        }

        public FireData RetrievedData()
        {
            return data; 
        }


    }

And call to method WriteStream in Default.aspx.cs:

FirebaseConnection firebaseConnection =
                new FirebaseConnection("mysecret",
                "https://mydb.firebaseio.com/");

public async Task UpdateFireDB()
        {
            Random rnd = new Random();
            firebaseConnection.WriteStream(new FireData(0, rnd.Next(1, 50)));
        }

        protected void Page_Load(object sender, EventArgs e)
        {


            RegisterAsyncTask(new PageAsyncTask(UpdateFireDB)); 

        }

Finally, here is the class used to handle the data:

 public class FireData
    {
        public int Id { get; set; }
        public double Value { get; set; }

        public FireData(int identity, double inputValue)
        {
            Id = identity;
            Value = inputValue; 
        }

        public FireData()
        {

        }
    }

Could somebody please shed some light on what I might have done wrong?

Many Thanks,

Ben.

BStanway
  • 7
  • 6
  • An NRE is very clear - you tried to call a method on a `null` variable. You don't provide the full exception, which would contain the call stack, so one can only guess that `response` is `null`. There's at least on big problem in this code, the use of `async void`. Such methods are *only* meant for asynchronous event handlers. The can't be awaited which means `FirebaseConnection` may already be garbage collected by the time `SetTaskAsync` returns – Panagiotis Kanavos Oct 02 '18 at 08:08
  • I'd bet the compiler already generated a warning for `async Task UpdateFireDB` saying it doesn't contain `await` so it will be executed synchronously. Change the `async void` methods to `async Task` and await them, eg in `UpdateFireDB` – Panagiotis Kanavos Oct 02 '18 at 08:09
  • You *haven't* followed the code in the snippets. You didn't await the methods, used `async void`. You didn't post the exception either - that NRE could be raised deep inside `SendTaskAsync` or `ReceiveAs` because the connection was already closed due to that `async void`. You can get the full exception including the call stack quite easily with `Exception.ToString()` – Panagiotis Kanavos Oct 02 '18 at 08:12
  • I have made the necessary changes, although I am still receiving the same null object reference error. You might be onto something with regards to garbage collection - though I wouldn't be too sure. Thanks for your help by the way. This comment was added before I saw the third comment. – BStanway Oct 02 '18 at 08:22
  • Post the exception! The call stack will show what threw it immediatelly – Panagiotis Kanavos Oct 02 '18 at 08:23
  • Exception thrown: 'System.NullReferenceException' in RestSharp.dll System.NullReferenceException: Object reference not set to an instance of an object. at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](IRestResponse response) at FireSharp.Extensions.ObjectExtensions.ReadAs[T](IRestResponse response) at FireSharp.Response.FirebaseResponse.ResultAs[T]() at BluzoneDemo.FirebaseConnection.d__8.MoveNext() in C:\Users\STANWAYB\Source\Repos\BluzoneDemo\BluzoneDemo\FirebaseConnection.cs:line 59 – BStanway Oct 02 '18 at 08:30

0 Answers0