database - Initialising KeyValuePair List in C# class constructor, how should it be done? -


i have class contains list of key value pairs, used part of sqlite.net database. using sqlite.net extensions this, , tables made correctly.

however, when try create new register add database, given nullreferenceexception in constructor register. have debugged code , seems issue 'studentlist', when removed works correctly.

my question is, how should key value pair list instantiated? seems messing somewhere when initialise it. below register class 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 student student list register, marking string value '-', show register has not been taken yet.     //this value set status of student, show them present, awol, sick, late, etc.     public void addstudent(student student)     {         studentlist.add(new keyvaluepair<student, string>(student, "-"));     } } 

edit: below code try create register. above code statement checks make sure each field contains something. have no idea how leading null ref. wrong this?

        var student = new student()         {             studentid = "setup",             username = "setup",             firstname = "setup",             lastname = "setup",             image = "check.png"         };          //set values text fields new register         var register = new register()         {             //change 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 stuff in comments below web db upload below adds register local db.         var result = database.db.createregister(register); 

edit: added few more lines of code (above) show how register passed db. below stack trace exception thrown:

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):     @ 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):     @ java.lang.reflect.method.invoke(native method) 02-09 14:40:55.959 e/androidruntime( 2312):     @ java.lang.reflect.method.invoke(method.java:372) 02-09 14:40:55.959 e/androidruntime( 2312):     @ 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 instance of object 02-09 14:40:55.959 e/androidruntime( 2312):   @ registrationapp.registerpage..ctor (int32 id) <0xdb47ac68 + 0x000bc> in <filename unknown>:0  02-09 14:40:55.959 e/androidruntime( 2312):   @ 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 previous location exception thrown --- 

also add (this may part of problem), when adding register database (the point exception occurs), code used:

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;         }     } 


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -