在C#类构造函数中初始化KeyValuePair列表,应该怎么做?

时间:2016-02-08 20:28:13

标签: c# database list sqlite key-value

我有一个包含Key Value Pairs列表的类,用作SQLite.Net数据库的一部分。我正在使用SQLite.Net Extensions,表格都是正确的。

但是,当我尝试创建一个新的Register以添加到数据库时,我在Register的构造函数中给出了NullReferenceException。我已经调试了代码,似乎问题出现在' StudentList',当删除它时,一切正常。

我的问题是,如何实例化键值对列表?当我初始化它时,我似乎搞砸了。下面是我的注册类,包含list和constuctor:

public class Register
{
    [PrimaryKey, AutoIncrement]
    public int RegisterID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Location { get; set; }
    public DateTime Date { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public Boolean Recurring { get; set; }

    [ManyToMany(typeof(RegStudent))]
    public List<KeyValuePair<Student, string>> StudentList { get; set; }

    public Register()
    {
        this.StudentList = new List<KeyValuePair<Student, string>>();
    }

    //Adds a student to the Student List for this register, marking the string value as '-', to show the register has not been taken yet.
    //This value will be set to the status of the student, to show them as Present, AWOL, Sick, Late, etc.
    public void addStudent(Student student)
    {
        StudentList.Add(new KeyValuePair<Student, string>(student, "-"));
    }
}

修改 下面是我实际尝试创建寄存器的代码。在此代码上方是一个语句,它检查以确保每个字段实际包含某些内容。我不知道这是如何导致null ref。这有什么不对吗?

        var student = new Student()
        {
            StudentID = "Setup",
            Username = "Setup",
            FirstName = "Setup",
            LastName = "Setup",
            Image = "check.png"
        };

        //set values from text fields to the new register
        var register = new Register()
        {
            //Change this to something unique, database should automatically set this.
            Name = registerName.Text,
            Type = registerType.Text,
            Location = registerLocation.Text,
            Date = datePicker.Date,
            StartTime = startTimePicker.Time.ToString(),
            EndTime = endTimePicker.Time.ToString(),
            Recurring = switcher.IsToggled,
            StudentList = new List<KeyValuePair<Student, string>>()
            {
                new KeyValuePair<Student, string>(student, "-")
            }
        };

        //Do the stuff in comments below for web db upload below adds register to local db.
        var result = database.db.CreateRegister(register);

编辑:添加了几行代码(上图),以显示寄存器如何传递给db。下面是抛出异常的堆栈跟踪:

02-09 14:40:55.959 E/AndroidRuntime( 2312): FATAL EXCEPTION: main
02-09 14:40:55.959 E/AndroidRuntime( 2312): Process: com.companyname.RegistrationApp, PID: 2312
02-09 14:40:55.959 E/AndroidRuntime( 2312): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
02-09 14:40:55.959 E/AndroidRuntime( 2312):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
02-09 14:40:55.959 E/AndroidRuntime( 2312): Caused by: java.lang.reflect.InvocationTargetException
02-09 14:40:55.959 E/AndroidRuntime( 2312):     at java.lang.reflect.Method.invoke(Native Method)
02-09 14:40:55.959 E/AndroidRuntime( 2312):     at java.lang.reflect.Method.invoke(Method.java:372)
02-09 14:40:55.959 E/AndroidRuntime( 2312):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
02-09 14:40:55.959 E/AndroidRuntime( 2312):     ... 1 more
02-09 14:40:55.959 E/AndroidRuntime( 2312): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
02-09 14:40:55.959 E/AndroidRuntime( 2312):   at RegistrationApp.RegisterPage..ctor (Int32 id) <0xdb47ac68 + 0x000bc> in <filename unknown>:0 
02-09 14:40:55.959 E/AndroidRuntime( 2312):   at RegistrationApp.CreateRegisterPage+<OnCreateButtonClicked>d__1d.MoveNext () <0xdb472000 + 0x00933> in <filename unknown>:0 
02-09 14:40:55.959 E/AndroidRuntime( 2312): --- End of stack trace from previous location where exception was thrown ---

另外要添加(这实际上可能是问题的一部分),当将注册表添加到数据库(发生异常的点)时,将使用以下代码:

public Boolean CreateRegister(Register register)
    {
        try
        {
            var newReg = new Register
            {
                Name = register.Name,
                Type = register.Type,
                Location = register.Location,
                Date = register.Date,
                StartTime = register.StartTime,
                EndTime = register.EndTime,
                Recurring = register.Recurring,
                StudentList = register.StudentList
            };
            _connection.Insert(newReg);
            return true;
        }
        catch
        {
            return false;
        }
    }

0 个答案:

没有答案