*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

Discussion in 'iOS Programming' started by Manesh, Mar 24, 2015.

  1. Manesh, Mar 24, 2015
    Last edited: Mar 24, 2015

    Manesh macrumors member

    Joined:
    Oct 30, 2013
    #1
    Can anyone tell me whats wrong with my code?
    Im getting a
    Code:
    "*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'"
    I have two tableviews, one showing who I'm connected to via MP connectivity, and the other showing the files in my documents directory.
    I know this is because its saying that my array has 0 objects, and I'm trying to access the object at index 1.
    But I have a file in my documents directory.
    when the app closes and opens again, the tableview is displayed correctly, but as soon as something is deleted, it presents me with the error.

    Help would be great!

    Code:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (tableView == _tblConnectedDevices){
        return [_arrConnectedDevices count];
        }
        if ([filePathsArray count] > 1){
            return [filePathsArray count];
        }
        else 
        return 1;
    }
    
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (tableView == _tblConnectedDevices){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
        }
        
        cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];
        
        return cell;
        } else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
            if ([filePathsArray count] == 0){
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
                NSLog(@"top %i",indexPath.row);
            }
            if ([filePathsArray count] > 0) {
                NSLog(@"bot %i",indexPath.row);
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
                filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
                NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
                NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
                cell.textLabel.text = last2;
                NSLog(@"bot %i",indexPath.row);
            }
            return cell;
        }
        
    }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        fileName = cell.textLabel.text;
    }
    
    -(IBAction)deleteFile:(id)sender{
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask,
                                                             YES);
        NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt", fileName]];
        NSError *error;
            [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
        [filePathsArray count];
        [_cellView reloadData];
    }
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    Code:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (tableView == _tblConnectedDevices){
        return [_arrConnectedDevices count];
        }
        if ([filePathsArray count] > 1){
            return [filePathsArray count];
        }
        else 
        return 1;
    }
    
    The 1st problem I see is that you will never get to the "if ([filePathsArray count] > 1)" if the 1st condition is true because at that point, you've left the method.

    You are checking for a different condition after a return statement that is checking something else.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Also, if [filePathsArray count] == 0, you're still returning 1.
     

Share This Page