|Feb 3, 2013, 09:53 AM||#1|
I have an encoding syntax error that crashes an app with with NSString
I have an encoding syntax error that crashes an app with a "*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'
here's the code:
// encoding fix cell.titleLabel.text = [[NSString stringWithCString:[[item objectForKey:@"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding]]capitalizedString];
Any idea how to fix this?
Last edited by maflynn; Feb 3, 2013 at 12:30 PM.
|Feb 3, 2013, 10:28 AM||#3|
Break it down.
Does [item objectForKey:@"main_tag"] return nil or a valid object? Exactly what type is the object?
Does cStringUsingEncoding:NSISOLatin1StringEncoding return a valid C string? Exactly what does it show up as (e.g. NSLog it with the proper conversion specifier)?
What does NSString stringWithCString: return? Again, verify what you expect using the debugger or NSLog.
What does capitalizedString return?
We know what you can expect each of those steps to do, but what does each one actually do? Where's your evidence that each step is doing what it should?
If you have to add some intermediate variables to allow seeing each step's values in the debugger, or NSLog'ing them, then add the intermediate variables.
Right now, you've used all your skill to write a complex expression. What skill will you now use to debug this complex expression? Break the complex problem down into a series of solvable sub-problems, then solve each sub-problem in turn. That's how you fix complex problems: by breaking them down.
|Feb 3, 2013, 10:54 AM||#4|
each NSLog(@"cell.titleLabel.text %@",cell.titleLabel.text);
[item objectForKey:@"main_tag"] outputs the name of a title, occasionally it encounters a NULL value. When the cstring encoding is performed on it there's a crash.
the NSLog output is:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithCString:encoding:]: NULL cString'
|Feb 3, 2013, 11:47 AM||#5|
Show your actual code, and the actual output produced by the code.
A C string is not an object. Therefore, "%@" in the format string will not work with it.
An "occasional" NULL value is enough to make the entire series of steps fail. Exactly which step is producing the NULL?
I already know which step is crashing, but that's not the one producing the NULL. It's just the one where the NULL is fatal. Some step BEFORE the crash is producing a NULL. Find that step, and find out why it's producing a NULL.
If you're expecting a NULL from that step, then why aren't you handling the possibility of a NULL?
If you're not expecting a NULL, then your expectation is clearly wrong, or something else is not working before the code reaches the step that produces the NULL.
|Thread Tools||Search this Thread|
All times are GMT -5. The time now is 07:57 PM.