0

I know a question like this has been posted before, but I was trying to get a direct answer specific to my code. I have seen the other post and have followed it to get to this stage.

I have the (null) libc++abi.dylib: terminate called throwing an exception line in my log. I have traced it to this line of code:

normalCell.textLabel.text = [[displayArray objectAtIndex:indexPath.row] objectForKey:@"name"];

this is in my cellForRowAtIndexPath method which has this code:

static NSString *normalCellIdentifier = @"normalCell";
static NSString *topRowCellIdentifier = @"topRowCell";

UITableViewCell *normalCell = [tableView dequeueReusableCellWithIdentifier:normalCellIdentifier];
if (normalCell == nil)
{
    normalCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellIdentifier];
}

UITableViewCell *topRowCell = [tableView dequeueReusableCellWithIdentifier:topRowCellIdentifier];
if (topRowCell == nil)
{
    topRowCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topRowCellIdentifier];
}

// Configure the cell...
if (searchBar.selectedScopeButtonIndex != 0)
{
    normalCell.textLabel.text = [[displayArray objectAtIndex:indexPath.row] objectForKey:@"name"];
}

if (segmentedControl.selectedSegmentIndex == 0)
{
    if (indexPath.section == 0)
    {
        [self makeButtonsForTableView:tableView atIndexPath:indexPath withTableViewCell:topRowCell];

        [topRowCell addSubview:button1];
        [topRowCell addSubview:button2];

        topRowCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return topRowCell;            
    }
    else
    {
        return normalCell;
    }
}
else
{
    return normalCell;
}

The makeButtonsForTableView has all my code for the button, but that isn't the problem because I have removed that from the code and the problem still persists so I don't need to include that.

The displayArray varies its contents depending on the selectedSegmentIndex on a UISegmentedControl and on a UISearchBar's selected scope.

Each of the Arrays it gets its values from import from my website domain, the arrays are: modsArray itemsArray serversArray pluginsArray newItemsArray newBlocksArray newMobsArray

Thanks for any help you can give.

-- EDIT 22:40GMT - 27/09/2012 --

So I logged the contents of displayArray when the app loads, and this is the log

2012-09-27 22:39:56.539 AppName[22949:11303] (
)
2012-09-27 22:39:56.675 AppName[22949:11303] (
)

-- EDIT 23:13GMT - 27/09/2012 --

Interestingly, when I change tabs and go back the log changes from this:

2012-09-27 23:13:14.074 MinePedia[23853:11303] DisplayArray Count: 0
2012-09-27 23:13:14.074 MinePedia[23853:11303] IndexPath Row: 0
2012-09-27 23:13:14.355 MinePedia[23853:11303] DisplayArray Count: 0
2012-09-27 23:13:14.355 MinePedia[23853:11303] IndexPath Row: 1

to this

2012-09-27 23:13:14.074 MinePedia[23853:11303] DisplayArray Count: 1
2012-09-27 23:13:14.074 MinePedia[23853:11303] IndexPath Row: 1
2012-09-27 23:13:14.355 MinePedia[23853:11303] DisplayArray Count: 1
2012-09-27 23:13:14.355 MinePedia[23853:11303] IndexPath Row: 0

Anyone know what this means?

-- EDIT 23:42 - 27/09/2012 --

Ok so sort of a different problem than before, it now doesn't crash so thats been solved, thanks Mayur, but now on startup and until the tab is changed at the top, the scope and table view remains with the indexpath being larger than the count. How do I fix this?

21zach2
  • 159
  • 1
  • 2
  • 12
  • Are you sure you are getting an object back from [displayArray objectAtIndex:indexPath.row]? – Mark Sep 27 '12 at 21:25
  • I don't understand the question.. Do you mean does that have a value, when logged or something? – 21zach2 Sep 27 '12 at 21:26
  • I guess the most likely reason for the exception is that your array doesn't have enough elements, so `indexPath.row` is out of bounds... – omz Sep 27 '12 at 21:28
  • Have you checked to make sure that [displayArray objectAtIndex:indexPath.row] does not return nil? – Mark Sep 27 '12 at 21:29
  • I'm unable to log that because it crashes when it gets to the line where I am logging it, does that meant that the array has a problem with it. – 21zach2 Sep 27 '12 at 21:32
  • What's `displayArray`'s count before executing that line? – Carl Veazey Sep 27 '12 at 21:49
  • Well, your log of displayArray shows that it's empty. What are you returning in tableView:numberOfRowsInSection:? – rdelmar Sep 27 '12 at 21:51
  • To expand upon the comment of @CarlVeazey, you should log both displayArray.count and indexPath.row just before the trouble line to see if they are in sync. – rdelmar Sep 27 '12 at 21:56
  • Ok I shall let you know once I have done them and, @rdelmar, if the section is 0 then return topRowCell but if its 1 then normalCell. Why? – 21zach2 Sep 27 '12 at 22:01
  • This gets returned when logging the indexPath.row and displayArray.count: `2012-09-27 23:04:00.361 MinePedia[23453:11303] DisplayArray Count: 0 2012-09-27 23:04:00.362 MinePedia[23453:11303] IndexPath Row: 0 2012-09-27 23:04:00.648 MinePedia[23453:11303] DisplayArray Count: 0 2012-09-27 23:04:00.648 MinePedia[23453:11303] IndexPath Row: 1` – 21zach2 Sep 27 '12 at 22:04
  • @21zach2 that's your problem. Please see my answer for a suggested solution. – Carl Veazey Sep 27 '12 at 22:08
  • Wait a second, why are you calling `reloadData` from this method? EDIT: nevermind, that would never be executed, method returns before then. – Carl Veazey Sep 27 '12 at 22:12
  • I don't know, I removed it now, could you try and interpret what is wrong with my log thing when I change tab, the latest edit – 21zach2 Sep 27 '12 at 22:19
  • Either something is wrong with what you're returning in tableView:numberOfRowsInSection: or your array is losing members between the time that method is called and the time cellForRowAtIndexPath is called. cellForRowAtIndexPath shouldn't be called at all if tableView:numberOfRowsInSection: returns 0. – rdelmar Sep 28 '12 at 02:47

2 Answers2

1

Keep it as following :

normalCell.textLabel.text = [NSString stringWithFormat:@"%@",[[displayArray objectAtIndex:indexPath.row] objectForKey:@"name"]];

This way, even if null value comes up then it won't crash.

Also, always check if array is filled up, only then access the list, like this.

if (searchBar.selectedScopeButtonIndex != 0)
{
    if(indexPath.row < [displayArray count])
    {
         normalCell.textLabel.text = [NSString stringWithFormat:@"%@",[[displayArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    }
}
mayuur
  • 4,736
  • 4
  • 30
  • 65
  • I think the only way that line is going to crash is if `indexPath.row` exceeds the count of the array. Shouldn't be any issue with `nil` objects here. – Carl Veazey Sep 27 '12 at 21:49
  • That worked, sort of. But I still need my values to appear, the displayArray includes the values for the information already – 21zach2 Sep 27 '12 at 22:07
  • This helped the first problem but I now have a second one, try and fix that too? – 21zach2 Sep 27 '12 at 22:27
0

Per your update in the comments, your displayArray has no elements when you call objectAtIndex: on it. This causes an exception. You need to ensure that your tableView:numberOfRowsInSection: returns a number of rows for this section equal to displayArray's count.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • Did I perhaps misunderstand your comment? I thought you were saying that `displayArray`'s count was 0 at the time the cell text was assigned. If you are returning the `disaplyArray`'s count as the number of rows then that shouldn't happen. – Carl Veazey Sep 27 '12 at 22:13