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]