Forum Xamarin Xamarin.Android

NullReferenceException Query SQLite database with Where on a concatenated string property;

Hey,

I'm trying to select a record using the following code:

Location item = connection.Table<Location>().Where(l => l.Label.Equals(label)).FirstOrDefault();

This results in: System.NullReferenceException: Object reference not set to an instance of an object.

When I try the same, on a different property (Postcode), it all works fine also when no records are found.:

Location item = connection.Table<Location>().Where(l => l.Postcode.Equals(label)).FirstOrDefault();

This is the Location Class:

// These are the Locations where the Stock Take Sessions are done
public class Location : DomainModels, IComparable<Location>
{
    [JsonProperty("id"), PrimaryKey]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
    public int Number { get; set; }
    public string Postcode { get; set; }
    public string City { get; set; }
    public bool Completed { get; set; }

    [Ignore] // Removing this does not have an impact on the NullReferenceException
    public string Label
    {
        get
        {
            return Name + " - (" + Postcode + ")";
        }
    }

    public int CompareTo(Location other)
    {
        return Name.CompareTo(other.Name);
    }

    // Navigation property
    // One to many relationship with StockItems
    [OneToMany(CascadeOperations = CascadeOperation.All), Ignore]
    public List<StockItem> StockItems { get; set; }

    // Specify the foreign key to StockTakeSession
    [ForeignKey(typeof(StockTakeSession))]
    public int StockTakeSessionId { get; set; }

    // One to one relationship with StockTakeSession
    [OneToOne]
    public StockTakeSession StockTakeSession { get; set; }

}

What am I doing wrong?

Thanks for any suggestions!

Tagged:

Answers

  • MabroukMabrouk USMember ✭✭✭

    Hi,

    Try to check if your Name and Postcode properties are null, it can be like :

     [Ignore] // Removing this does not have an impact on the NullReferenceException
     public string Label => $"{Name ?? ""} - ({Postcode ?? ""})";
    

    (Sure the string format can be changed!)

    I hope that helps,
    Mabrouk.

  • Robert_VdVRobert_VdV USMember ✭✭

    Hey @Mabrouk ,

    Thank you for the quick response.

    Unfortunately this didn't make a difference in this case. Do you maybe have an other trick up you sleeve?

    What do you mean by "(Sure the string format can be changed!)"? I am quite new with this so any suggestions to improve my code are more than welcome!

    Thank you,
    Robert

  • MabroukMabrouk USMember ✭✭✭
    edited September 2017

    Hi @Robert_VdV

    I get it.. its because you ignore the Label property in SQLite table (Real saved one)..

    you try this : as Parameters dont use label but postcode and name

    Location item = connection.Table<Location>().Where(l => l.Postcode?.Equals(postcode) && l.Name?.Equals(name)).FirstOrDefault();

    Mabrouk.

  • Robert_VdVRobert_VdV USMember ✭✭

    Hey @Mabrouk ,

    The reason I use the Label property is because I like to fetch a record from the database for a SelectedItem of a Spinner. A Spinner doesn't have a key-value pair. So you can't just look for the selected Key and use it as Id in your query.

    Each value in the Spinner is created by using the Label property of the Location Object. So I thought it would be easiest to select a Location record by looking at the Label property. That is why I refer to select the record by Label and not a separate Name and Postcode.

    The reason why I Ignore the Label property is because the table is populated from an external source, by Deserializing JSON, which doesn't have the label property. I just commented out the [Ignore], but the exception persists.

    Thank you,
    Robert

Sign In or Register to comment.