.NET : Mission Add Object With API

Hi,

I have an issue trying to achieve this quest “Add Object With API”. I imported the nuget package in my project and when I try to add an object to the Person table I always have the same issue when I’m running this line :

var saveResult = Backendless.Data.Of("Person").Save(dict);

System.NullReferenceException : ‘Object reference not set to an instance of an object.’

Of is the table name, and Save is asking for a Dictonary in the Backendless library that’s why I’m not passing a simple json object like in Javascript.

Complete Code :

// Initialisation Backendless
                try
                {
                    string APP_ID = "xxxxxxxxxxxxxx";
                    string API_KEY = "xxxxxxxxxxxxxx";

                    Backendless.InitApp(APP_ID, API_KEY);
                }
                catch (Exception ex)
                {
                    // Gestion d'erreur initialisation Backendless
                    return StatusCode(500, "Error initializing Backendless");
                }

                // Créer objet
                var objToSave = new { name = "Joe", age = 25 };
                // Convertir en Dictionary
                var dict = new Dictionary<string, object>();
                dict["name"] = objToSave.name;
                dict["age"] = objToSave.age;

                try
                {
                    // Appel méthode problématique
                    var saveResult = Backendless.Data.Of("Person").Save(dict);
                }
                catch (Exception ex)
                {
                    // Gestion erreur sauvegarde 
                    Console.WriteLine("Error saving object: " + ex.Message);
                    return StatusCode(500, "Error saving object");
                }

                return StatusCode(200, "Object created");

Is anyone achieved this task using .NET ?
If so, can you provide a solution ?

Thanks :slight_smile:

Hi Alex,

At first glance, the logic looks correct. Could you check if you have API Tracking enabled on the Manage screen in Backendless Console?

Regards,
Mark

Hi Mark,

Thank you for your support.
API tracking was one of the first thing I checked before writing on the forum.
I’m still digging on this issue. I’ll let you know if I find out a solution.

Even when I try to retrieve data form this table, I have the same error message :

var saveResult = Backendless.Data.Of("Person").Find();

System.NullReferenceException : ‘Object reference not set to an instance of an object.’

I also checked more than once my API_KEY and APP_ID.

Regards,
Alex :slight_smile:

Hello, @AlexCar.

Is your app on EU or US server?

Regards, Nikita.

Hi @Nikita_Fedorishchev.

I think it’s on EU server as long as I’m seeing this.

image

Otherwise could you provide me the way I can check it ?

I can try to create a new app if you want ?

Update :
I tried to create a new app (in EU cluster) and i’m sill have the same error when I try to retrieve the table Person from my .NET web service.

Thank you
Alex

Hi,

I finaly found the solution.

The problem is, because I’m in the EU Cluster I need to add this single line to my code during the init part.

Like so :

// Initialisation Backendless
try
{
    string APP_ID = "xxxxxxxxxx";
    string API_KEY = "xxxxxxxxxx";

    Backendless.URL = "https://eu-api.backendless.com";
    Backendless.InitApp(APP_ID, API_KEY);
}
catch (Exception ex)
{
    // Gestion d'erreur initialisation Backendless
    return StatusCode(500, "Error initializing Backendless");
}

For improvement, the returned error should be more explicit. We shouldn’t have any messages coming from the system. It should be handled by backendless and backendless should deliver a personal message to users. Only from my opinion :smile:

Thanks for your support :slight_smile:
Alex

You found the solution yourself, faster than I could answer you :sweat_smile:.

Yes, you had to provide a URL as the US server is used by default.
The error displays the result of the method. Thanks for your feedback.

Regards, Nikita.

Yes, you pushed me an the right way :smile:

About the error system, it could have be handled in the library directly to avoid this none explicit message. As you say it’s the result of the method.

Thank you, regards.
Alex

Yes, it can be done. But it will not be possible to find out if the application exists without sending a request there.
We will think about what can be done.