0

I use the following code to connect to firebase and try to read data. The connection is successful but I am unable to read the data. It gives System.NullReferenceException:'Object reference not set to an instance of an object.

namespace GPSTrack
{


public partial class Form1 : Form
{
    IFirebaseConfig config = new FirebaseConfig
    {
        AuthSecret = "4xxxxxxxxxxxxxxxxEzs",
        BasePath= "nodxxxxxx.firebaseio.com"
    };
    IFirebaseClient client;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        client = new FireSharp.FirebaseClient(config);
        if(client!=null)
        {
            MessageBox.Show("Connection success");
        }
    }

    private async void  button1_Click(object sender, EventArgs e)
    {
        FirebaseResponse response = await client.GetTaskAsync("test");
        FBclass obj = response.ResultAs<FBclass>();

        MessageBox.Show(response.ToString());
    }
}
}

1 Answers1

0

The problem might be in the response creation, because you've only placed a parent node, called "test". If that's the only dataset/node in your firebase instance then it might not have returned an exception. If there is a single child node under the "test" parent node, then I would like to suggest hard coding that node in the get call:

FirebaseResponse response = await client.GetTaskAsync("test/childnode");
FBclass obj = response.ResultAs<FBclass>();

Or you can create a call to a textbox asking for a specific ID:

FirebaseResponse response = await client.GetTaskAsync($"test/{ textBox.Text }");
FBclass obj = response.ResultAs<FBclass>();
Iliass Nassibane
  • 651
  • 7
  • 15