0

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

1
  • Formatting your code properly (indent it by four spaces) will help people recognize your problem and give you a solution. Commented Dec 4, 2009 at 4:22

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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].

Comments

0

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

1 Comment

Or he's trying to append the int and it's 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.