Question

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.

Was it helpful?

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.

OTHER TIPS

Can u Check this code it will be works:

if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIndentifier]; countryLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(tableView.bounds)/2, 44)]; countryLbl.textColor = [UIColor blueColor]; countryLbl.textAlignment = NSTextAlignmentLeft; [cell.contentView addSubview:countryLbl]; [countryLbl setTag:11]; localNoLbl = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(tableView.bounds)/2, 0,CGRectGetWidth(tableView.bounds)/2, 44)]; localNoLbl.textColor = [UIColor blackColor]; localNoLbl.textAlignment = NSTextAlignmentCenter; [cell.contentView addSubview:localNoLbl]; [localNoLbl setTag:12];

    }

    [cell.contentView.subviews enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {


        if ([obj isKindOfClass:[UILabel class]]) {
           countryLbl = (UILabel *)[cell.contentView viewWithTag:11];

                countryLbl.text = [countryLocArray objectAtIndex:indexPath.row];
            localNoLbl = (UILabel *)[cell.contentView viewWithTag:12];
                localNoLbl.text=[locArray objectAtIndex:indexPath.row];

            NSLog(@"%@---%@-%@-%@",locArray,countryLocArray,countryLbl.text,localNoLbl.text);
    }
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top