2

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.

ayalcin
  • 877
  • 1
  • 8
  • 16
  • In Xcode, set a breakpoint on exceptions. Then you see exactly where this happens. You have an object of class NSNull somewhere. – gnasher729 Apr 09 '14 at 21:31
  • The message indicates that the `getLength` method was invoked on a null 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 – Paulw11 Apr 09 '14 at 21:34
  • Yes the problem is category.GroupName is null. Thanks Paulw11 – ayalcin Apr 09 '14 at 21:38
  • You should also enable NSZombies - http://michalstawarz.pl/2014/02/22/debug-exc_bad_access-nszombie-xcode-5/ – Paulw11 Apr 09 '14 at 21:39
  • @Hüseyin Avni YALÇIN :- this is same condition i am having with my project, difference is that my table view is static and i am getting this error when i start to type some thing on textField of cell :( no luck please share your experience ! here [here](http://stackoverflow.com/questions/34264796/nsnull-length-unrecognized-selector-sent-to-instance-on-keyboard-keystroke?noredirect=1#comment56271371_34264796) is my question for your reference ! – dip Dec 14 '15 at 11:17

2 Answers2

1

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.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Actually the error states that the `length` method was called, not `getLength`. And it was called on an instance of `NSNull`, not a "null object". – rmaddy Apr 09 '14 at 23:12
0

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);
    }
    }];