windows forms designer - C# Null reference exception and StreamReader -


i getting null reference exception when reading data txt file.

     public class appointments : list<appointment>         {             appointment appointment;              public appointments()             {              }              public bool load(string filename)             {                 string appointmentdata = string.empty;                 using (streamreader reader = new streamreader(filename))                 {                     while((appointmentdata = reader.readline()) != null)                     {                         appointmentdata = reader.readline();                         //**this null ref. exception thrown** (line below)                         if(appointmentdata[0] == 'r')                         {                             appointment = new recurringappointment(appointmentdata);                         }                         else                         {                             appointment = new appointment(appointmentdata);                         }                         this.add(appointment);                     }                     return true;                 }             } 

recurringappointment inherits appointments. file exists, file location correct. funny thing program working 30 min ago i've changed load method below u can see above :

 public bool load(string filename)         {             string appointmentdata = string.empty;             using (streamreader reader = new streamreader(filename))             {                 while((appointmentdata = reader.readline()) != null)                 {                     appointmentdata = reader.readline();                     if(appointmentdata[0] == 'r')                     {                         this.add(appointment = new recurringappointment(appointmentdata));                     }                     else                     {                         this.add(appointment = new appointment(appointmentdata));                     }                 }                 return true;             }         } 

now not work in either case.

your code reads 2 times @ each loop. means that, if file has odd number of rows when read last line of file, check against null inside while statement allows code enter loop following readline returns null string. of course trying read char @ index 0 of null string throw nre exception.

there problem of empty lines in file. if there empty line then, again reading @ index 0 throw index out of range exception

you fix code in way

public bool load(string filename) {     string appointmentdata = string.empty;     using (streamreader reader = new streamreader(filename))     {         while((appointmentdata = reader.readline()) != null)         {             if(!string.isnullorwhitespace(appointmentdata))             {                 if(appointmentdata[0] == 'r')                     this.add(appointment = new recurringappointment(appointmentdata));                 else                     this.add(appointment = new appointment(appointmentdata));             }         }         return true;     } } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -