1

update: I think the error is coming from a missing key in the data feed and not a key with a null value.

I've been running into a null object by the json interpreter and thought this code would error check and avoid the crash. It's not working though. I need to check to avoid this error message and crash:

imageurl string is <null>
2013-09-09 12:35:26.928 1000 [1539:690b] -[NSNull length]: unrecognized selector sent to instance

if (![imageUrlString isKindOfClass:[NSNull class]])
{

    imageUrlString = [userImageArray objectAtIndex:indexPath.row];


} else {

    cell.thumbnailImageView.image = [UIImage imageNamed:@"rest.png"];

}

fyi - it works if the image string exists and is only crashing when a null object is encountered.

the output from nslog:

when a null obj is encountered: imageurl string is (null)

when it works: imageurl string is http://******.com/uploads/users/35/profile.jpeg

thanks for help

user2588945
  • 1,681
  • 6
  • 25
  • 38
  • imageurl string is array or string? – iAppDeveloper Sep 09 '13 at 11:41
  • imageurlstring is an NSString declared in the .h file. thanks! – user2588945 Sep 09 '13 at 11:43
  • if([imageUrlString isEqual: [NSNull null]]) NSLog(@"null"); try this – iAppDeveloper Sep 09 '13 at 11:45
  • 1
    You test `imageUrlString` for `NSNull`, but then *replace* it by `[userImageArray objectAtIndex:indexPath.row]` - that looks suspicious at least! - Which line produces the "imageurl string is " output, and which line exactly crashes? – Martin R Sep 09 '13 at 11:46
  • @MartinR the first part of the if statement causes the crash if a null obj is encountered imageUrlString = [userImageArray objectAtIndex:indexPath.row]; the output from nslog: like this imageurl string is (null) instead of imageurl string is http://******.com/uploads/users/35/profile.jpeg – user2588945 Sep 09 '13 at 11:54
  • @user2588945: You give incomplete or contradictory information. In your question, the output is "", now it is "(null)". You should show the complete code with the NSLog statements and the corresponding output. Otherwise people can only *guess* about your problem. – Martin R Sep 09 '13 at 12:20
  • @MartinR I think the error is coming from a missing key in the data feed and not a key with a null value. – user2588945 Sep 09 '13 at 12:28

1 Answers1

1

This should do the trick. It don't think that your check for NSNull is not the proper technique to check for a null. Try one of the checks in the first if statement:

/* Either of these checks in the if statement should work */
if ((NSNull *)imageUrlString != [NSNull null] || [@"<null>" isEqualToString:imageUrlString]) {
    imageUrlString = [userImageArray objectAtIndex:indexPath.row];
} else {
    cell.thumbnailImageView.image = [UIImage imageNamed:@"rest.png"];
}
Christian Di Lorenzo
  • 3,562
  • 24
  • 33