ios - array is not empty but still getting the error index 0 bounds -


i using sqlite , want save name, address, , phone text fields them show in next view controller when "show details" button clicked in 1st vc.

i placed "save" , "show details" button in 1st vc, "previous" , "next" button in 2nd vc. whenever click on "show details" getting error message:

index 0 beyond bounds empty array.

however, see array not empty. want store student details in array.

- (void)viewdidload {     [super viewdidload];        nsstring *homedirectory = nshomedirectory();     nsstring *documentsdirectorypath = [homedirectory stringbyappendingpathcomponent:@"documents"];     self.dbfilepathindocuments = [documentsdirectorypath stringbyappendingpathcomponent:@"details.db"];     self.studentdetails = [[nsmutablearray alloc]init];      nsstring *selectquery = [nsstring stringwithformat:@"select name,address,phone contacts"];     sqlite3_open([self.dbfilepathindocuments utf8string], &database);     sqlite3_prepare_v2(database, [selectquery utf8string], -1,&selectstatement, null);     while (sqlite3_step(selectstatement) == sqlite_row)     {         nsmutabledictionary *studentdict = [[nsmutabledictionary alloc]init];         nsstring *name = [nsstring stringwithformat:@"%s",sqlite3_column_text(selectstatement, 0)];         nsstring *address = [nsstring stringwithformat:@"%s",sqlite3_column_text(selectstatement, 1)];         nsstring *phone = [nsstring stringwithformat:@"%s",sqlite3_column_text(selectstatement, 2)];         [studentdict setobject:name forkey:@"name"];         [studentdict setobject:address forkey:@"address"];         [studentdict setobject:phone forkey:@"phone"];         [self.studentdetails addobject:studentdict];         nslog(@"student is:%@",self.studentdetails);     }      sqlite3_finalize(selectstatement);     sqlite3_close(database);      self.namelabel.text = [[self.studentdetails objectatindex:0] valueforkey:@"name"];     self.addresslabel.text = [[self.studentdetails objectatindex:0] valueforkey:@"address"];     self.phonelabel.text = [[self.studentdetails objectatindex:0] valueforkey:@"phone"];      currentstudentindex = 0;  }  - (ibaction)clickprevious:(id)sender {     if(currentstudentindex <=0)     {         currentstudentindex = 0;     }else     {         currentstudentindex = currentstudentindex - 1;     }       self.namelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"name"];     self.addresslabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"address"];     self.phonelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"phone"]; }   - (ibaction)clicknext:(id)sender {      if(currentstudentindex >= [self.studentdetails count] - 1)     {         currentstudentindex = [self.studentdetails count] - 1;     }else     {         currentstudentindex = currentstudentindex + 1;     }      self.namelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"name"];     self.addresslabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"address"];     self.phonelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"phone"]; } 

the issue accessing array self.studentdetails if it's empty. cause exception.

first limit setting of labels single method , check array access succeed before attempting it:

- (void)updatelabels {     if (currentstudentindex >= [self.studentdetails count])         return;     self.namelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"name"];     self.addresslabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"address"];     self.phonelabel.text = [[self.studentdetails objectatindex:currentstudentindex] valueforkey:@"phone"]; } 

and use method in 3 places set labels. example:

- (ibaction)clickprevious:(id)sender {     currentstudentindex--;     [self updatelabels]; }  - (ibaction)clicknext:(id)sender {     currentstudentindex++;     [self updatelabels]; } 

in viewdidload method use code:

... sqlite3_finalize(selectstatement); sqlite3_close(database); currentstudentindex = 0; [self updatelabels]; 

after you're gonna want work on enabling/disabling buttons depending on whether there next or previous student view make using app more intuitive.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -