Search code examples
iosobjective-cuitableviewnsnull

Weird error in UITableView NSInvalidArgumentException reason: [NSNull length], when it is not null


I am having a crush problem. This is the first thing that I couldn't find the solution online. Please help if you had same problem before. Thanks.

Error message:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x3a072a70'

Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *lblCategoryName;
    UILabel *lblGroupName;
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        lblCategoryName = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        [lblCategoryName setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
        [lblCategoryName setTag:1];
        [cell.contentView addSubview:lblCategoryName];

        lblGroupName = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 20)];
        [lblGroupName setFont:[UIFont fontWithName:@"HelveticaNeue-Italic" size:10]];

        [lblGroupName setTag:2];
        [cell.contentView addSubview:lblGroupName];
    }

    POCategory *category = [self.variantCategories objectAtIndex:indexPath.row];

    lblCategoryName = (UILabel*)[cell.contentView viewWithTag:1];
    [lblCategoryName setText:category.CategoryName];

    lblGroupName = (UILabel*)[cell.contentView viewWithTag:2];
    [lblGroupName setText:category.GroupName];

    return cell;
}

The weird thing is; It is working perfectly when I comment out the following lines OR when my array "variant.Categories" is empty. (I can even fill the tableview from another functions) However it is not crushing in these lines. And I can see the object while debugging. It is not empty. Everything looks normal.

    //POCategory *category = [self.variantCategories objectAtIndex:indexPath.row];

    //lblCategoryName = (UILabel*)[cell.contentView viewWithTag:1];
    //[lblCategoryName setText:category.CategoryName];

    //lblGroupName = (UILabel*)[cell.contentView viewWithTag:2];
    //[lblGroupName setText:category.GroupName];

"variantCategories" array is defined like this in my UITableViewController class, and this is the only variable that I am passing to this view controller.

    @property (nonatomic, retain) NSArray *variantCategories;

I tried to make it "strong" instead of "retain" but it didn't work.

Any kind of help highly appreciated.

Thanks.


Solution

  • The message indicates that the length method was invoked on an NSNull object. Since the length method will most likely be invoked on an NSString and commenting the two setText lines addresses your issue, at a guess I would say that either GroupName or CategoryName is NSNull. You should make sure that these two properties are defined as strong on your Category object.