I have an encoding syntax error that crashes an app with with NSString

Discussion in 'iOS Programming' started by hanuman99, Feb 3, 2013.

  1. hanuman99, Feb 3, 2013
    Last edited by a moderator: Feb 3, 2013

    macrumors newbie

    Joined:
    Nov 10, 2010
    #1
    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!
     
  2. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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.
     
  3. macrumors 603

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Nov 10, 2010
    #4
    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'
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #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.
     

Share This Page