views:

333

answers:

3

hi experts, i try to append from NSMutableArray but the below exception, actually first 2 loop its giving result but the 3rd loop giving this exception

2009-12-04 12:01:19.044 AppBuzz[14562:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString appendString:]: nil argument'
2009-12-04 12:01:19.057 AppBuzz[14562:207] Stack: (
    820145437,
    837578260,
    819694387,
    819694291,
    814894619,
    814804961,
    17445,
    23977,
    18651,
    19281,
    862009284,
    9035,
    861597328,
    861596264,
    861928960,
    861926972,
    861925524,
    858687888,
    819893547,
    819891231,
    861592584,
    861585968,
    8749,
    8612
)
terminate called after throwing an instance of 'NSException'

my code as below

 for (int i = 0; i < [student count]; i++) 
 {
  if([student objectAtIndex:i] != NULL)
  { 
  dic = [student objectAtIndex:i];
  tempName = [dic objectForKey:@"NAME"];    
  tempAvgMark = [[dic objectForKey:@"AVG_MARK"] intValue]; 
  [data appendString:@"{\"name\":\""];
  [data appendString:tempName];                       // here i'm having prob
  [data appendString:@"\",\"avg_mark\":\""];
  //[data appendString:tempAvgMark];           
  [data appendString:@"\"}"];  
  }  
 }
        NSLOG(@"Result - %@",data);

can anyone help for 1) to append [data appendString:tempName]; 2) to append int value ([data appendString:tempAvgMark]; ) into data

thanks

A: 

It appears from this message: NSCFString appendString:]: nil argument' that one of the dictionary values is null

ennuikiller
Or he's trying to append the int and it's 0.
Chuck
+1  A: 

The object for the key @"NAME" in the dictionary has to be a string, and to append an int you can use [string appendFormat:@"%d", someInt].

Chuck
+3  A: 

You're trying to append a nil, which is illegal. You can easily get around this by changing the line in question to the following:

[data appendString:(tempName == nil ? @"" : tempName)];

Or replace that empty string with whatever else you want. To append a non-string value, convert it to a string like this:

[data appendString:[NSString stringWithFormat:@"%i", tempAvgMark]];

If tempAvgMark is not an 'int', you'll have to change the %i. For example, for NSNumbers, use %@ instead.

Ian Henry