How to Properly Model Data and Handle Parse Queries in .NET MAUI?

Hi

I have a question regarding how to create models with linked data and retrieve them correctly to avoid encountering NullReferenceException errors. Could you please share best practices or examples on setting up and accessing related data in a safe manner?

new ParseClient(new ServerConnectionData
        {
            ApplicationID = "",
            ServerURI = "",
            Key = "",
        }).Publicize();
ParseClient.Instance.RegisterSubclass(typeof(EventType));
ParseClient.Instance.RegisterSubclass(typeof(Event));
[ParseClassName("Event")]
public class Event : ParseObject
{
    
    public Event()
    {
    }
    
    
    [ParseFieldName("title")]
    public string Title
    {
        get => GetProperty<string>(null, nameof(Title));
        set => SetProperty(value, nameof(Title));
    }
    
    [ParseFieldName("description")]
    public string Description
    {
        get => GetProperty<string>(null, nameof(Description));
        set => SetProperty(value, nameof(Description));
    }
    
    [ParseFieldName("eventType")]
    public ParseObject EventType
    {
        get => GetProperty<EventType>(null, nameof(EventType));
        set => SetProperty(value, nameof(EventType));
    }
}
[ParseClassName("EventType")]
public class EventType : ParseObject
{
    public EventType()
    {
    }
    
    [ParseFieldName("name")]
    public string Name
    {
        get => GetProperty<string>(null, nameof(Name));
        set => SetProperty(value, nameof(Name));
    }
}
var events = await ParseClient.Instance
                 .GetQuery<Event>()
                 .GetAsync("vGL4v8VYsE");

//NullReferenceException errors

ParseQuery<ParseObject> query = ParseClient.Instance.GetQuery("Event");
            var result = await query.GetAsync("vGL4v8VYsE");

//NullReferenceException errors

Data in the database

{
  "results": [
    {
      "title": "Test 6",
      "description": "New descriptions",
      "eventType": {
        "name": "Бег",
        "createdAt": "2025-04-07T17:50:47.703Z",
        "updatedAt": "2025-04-08T14:49:58.292Z",
        "objectId": "xqDSKujsM7",
        "__type": "Object",
        "className": "EventType"
      },
      "objectId": "vGL4v8VYsE"
    }
  ]
}

Thank you.

Hi
Consider querying directly as

CLASS_NAME event= await ParseClient.Instance.GetQuery<CLASS_NAME>().WhereEqualTo(“objectId”,key.ObjectId )
.FirstAsync();

since you have the class registered already.

The problem occurs when User == null
Queries to classes without relations work perfectly. However, when fetching objects that include pointer fields (e.g., EventType), we encounter a NullReferenceException during deserialization.

The server returns valid data with nested objects, but the SDK fails to map them to their respective models.

System.NullReferenceException: Object reference not set to an instance of an object.
   at Parse.ParseObject.OnFieldsChanged(IEnumerable`1 fields) in /Parse-SDK-dotNET-master/Parse/Platform/Objects/ParseObject.cs:line 1121

If await Services.GetCurrentUser() returns null, then we get an exception

public async Task<IEnumerable<T>> FindAsync(CancellationToken cancellationToken)
    {
        EnsureNotInstallationQuery();
        var result = await Services.QueryController.FindAsync(this, await Services.GetCurrentUser(), cancellationToken).ConfigureAwait(false);
        return result.Select(state => Services.GenerateObjectFromState<T>(state, ClassName));
    }

All received objects are public and we can’t show the user on the main event page until they log in, which is not very convenient