c# - Baffling IndexOutOfBoundsArray Exception -


can 1 of fine folks tell me possibly causing c# method throw indexoutofbounds exception? appreciated.

    public bool populatestudents(string path)   //decided return bool if successful reading file.     {         thestudentlist = new list<student>(); //create instance..         string text = null;         fileinfo source = new fileinfo(@path);         bool success = true;         try         {             streamreader r = source.opentext();             text = r.readline();             string[] splittext = new string[23];             student currentstudent = new student();             while (text != null)             {                 splittext = text.split(',');                 currentstudent = new student(splittext[0], splittext[1], splittext[2]);                 (int = 0; < 20; += 2)                 {                     currentstudent.entergrade(int.parse(splittext[i + 3]), int.parse(splittext[i + 4]));                 }                 currentstudent.calgrade();                 thestudentlist.add(currentstudent);                 text = r.readline();             }             r.close();         }         catch (exception exc)         {             success = false;             console.writeline(exc.message);         }          return success;      } 

sample input file:

0199911,bill,gates,27,30,56,60,0,30,83,100,57,60,0,30,59,60,0,30,59,60,88,100 0199912,steve,jobs,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100 0199913,marc,andresen,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100 0199914,larry,ellisen,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100 

edit: of answers great , appreciated, turns out had empty blank space @ end of text file. point out responses provided fix problem if keep blank space @ end. :)

whenever read line less 23 commas. empty line in end.

you should do

if (splittext.length<24) {   warnlogordosomethingelse(text);   continue; } 

immediately after

splittext = text.split(','); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -