0

I have the following piece of code in my project (Xamarin cross-platform), the line that is giving me the error is:

med.Medicine_Incident.Add(med_inc);

I am not sure how the line is null-referenced, because as we can see, I have set the values to the med_inc properties (time and dosage), also I have set the values for the med properties (name and description). (Medicine and Medicine_Incident classes at the bottom)

this.BindingContext = new Medicine();

var med = (Medicine)BindingContext;

this.BindingContext = new Medicine_Incident();
var med_inc = (Medicine_Incident)BindingContext;

Medicine_Name = "Insulin";
Medicine_Desc = "Because normal digestion interferes with insulin taken by mouth, insulin must be injected";
med.Medicine_Name = Medicine_Name;
med.Medicine_Desc = Medicine_Desc;

med_inc.Dosage = Insulin_Dosage;

if (Insulin_Morning_Toggled)
{
    //Morning 8 a.m , year 2015 December 31
    Time = new DateTime(2050, 12, 31, 08, 00, 0);
    med_inc.Time = Time;

    med.Medicine_Incident.Add(med_inc);
}

Medicine class:

public class Medicine : User_Profiles
{
    [Ignore]
    public virtual ICollection<Medicine_Incident> Medicine_Incident { get; set; }

    // This is my link to the User_Profiles PrimaryKey; A foreigh Key
    //public int User_ProfilesId { get; set; }

    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Medicine_Name { get; set; }
    public string Medicine_Desc { get; set; }

    public Medicine()
    {
        Medicine_Name = "";
        Medicine_Desc = "";
    }

    public Medicine(string medicine_Name, string medicine_Desc)
    {
        Medicine_Name = medicine_Name;
        Medicine_Desc = medicine_Desc;
    }
}

Medicine_Incident class:

public class Medicine_Incident : Medicine
{
    //[PrimaryKey, AutoIncrement]
    //public int Id { get; set; }

    // This is my link to the medicine PrimaryKey; A foreigh Key
    //public int MedicineId { get; set; }

    public DateTime Time { get; set; }
    public int Dosage { get; set; }

    public Medicine_Incident()
    {
        // (1/1/0001 12:00:00 AM)
        Time = default(DateTime);
        Dosage = 0;
    }

    public Medicine_Incident(DateTime medicine_Time, int dosage)
    {
        Time = medicine_Time;
        Dosage = dosage;
    }
}

Could the error arise from the [ignore] tag in this line of the Medicine class:

[Ignore]
public virtual ICollection<Medicine_Incident> Medicine_Incident { get; set; }

If it is so, then there is another problem, as I am unable to use that line without [ignore]. If I do, I get a different error:

System.NotSupportedException: Don't know about System.Collections.Generic.ICollection`1[LoginSystem.Models.Medicine]

Massimiliano Kraus
  • 3,214
  • 5
  • 20
  • 40
TigerLionCheetah
  • 178
  • 1
  • 17
  • Where do you initialize the `Medicine_Incident` property on the `Medicine` class? Seems like that's what's `null` here. – David Jun 19 '17 at 16:38
  • I am sorry i didnt quite get it, how should that initialization look like? – TigerLionCheetah Jun 19 '17 at 16:41
  • in the constructor of Medicine, you need to initialize the Medicine_Incident property, otherwise it will be null when you try to add to it. – Jason Jun 19 '17 at 16:42
  • Normally I would initialize a property in the object's constructor. What should that property be? (Since `ICollection` is an interface, you need a concrete type.) For example: `this.Medicine_Incident = new List();` – David Jun 19 '17 at 16:42
  • Just to let you know, it is better if you write your first lines like this ``var med = new Medicine(); this.BindingContext = med; var med_inc =new Medicine_Incident(); this.BindingContext = med_inc;`` And you do realize you are overwriting your this.BindingContext? – Rand Random Jun 19 '17 at 16:50
  • Just realized... it is kind of funny to see that you didnt initialize the ICollection but you are initializing the DateTime property with ``default(DateTime)`` which is a useless line of code since DateTime is a struct and that doesnt have to be initialized. – Rand Random Jun 19 '17 at 17:07

0 Answers0