Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hanuman99

macrumors newbie
Original poster
Nov 10, 2010
5
0
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'
" crash

here's the code:
Code:
// encoding fix
cell.titleLabel.text = [[NSString stringWithCString:[[item objectForKey:@"main_tag"] cStringUsingEncoding:NSISOLatin1StringEncoding]]capitalizedString];

apparently there's a double conversion occurring and that's why the call to cStringUsingEncoding:NSISOLatin1StringEncoding returns NULL).

Any idea how to fix this?

thanks!
 
Last edited by a moderator:

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
Break that combined line into individual lines. It will mle analyzing the problem easier and if there is a conflict may even fix the issue.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Break it down.

Does [item objectForKey:mad:"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.
 

hanuman99

macrumors newbie
Original poster
Nov 10, 2010
5
0
each NSLog(@"cell.titleLabel.text %@",cell.titleLabel.text);

[item objectForKey:mad:"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'
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.