0

I've looked over the site for a while and I saw a few things but nothing worked for me. Extreme newbie here though.

I'm trying to make a 2 component picker, populated from arrays, populated from a dictionary, populated from a plist. The components populate, and I have a button that spins one component. The second button is meant to check answers (the first component has states which they try to match with capitals in the second component), but this is where it always crashes.

The error I get is as follows:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'

* First throw call stack: (0x14b3022 0xeb3cd6 0x145ba48 0x145b9b9 0x916169 0x916ab4 0x3036 0x14b4e99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x139def5 0x1487195 0x13ebff2 0x13ea8da 0x13e9d84 0x13e9c9b 0x139c7d8 0x139c88a 0x16626 0x27ad 0x2715) terminate called throwing an exception(lldb)

I'm not sure where I'm going wrong. I've done this with hard coded arrays, and it works fine. The code for the button is like so (please ignore the switch code, I haven't quite figured out how to make that work yet, I want the screen to turn green for correct answers and red for incorrect, but I'm more concerned with the app crashing!):

-(IBAction)checkAnswer; { NSInteger StateRow = [pickerCapital selectedRowInComponent:karrayStateComponent]; NSInteger CapitalRow =[pickerCapital selectedRowInComponent:karrayCapitalComponent];

NSString *state = [arrayState objectAtIndex:StateRow];
NSString *capital = [arrayCapital objectAtIndex:CapitalRow];

NSLog (@"%i",[pickerCapital selectedRowInComponent:karrayCapitalComponent]);
NSLog(@"%i", [pickerCapital selectedRowInComponent:karrayStateComponent]);

NSString *Title = [[NSString alloc]initWithFormat:@"You have selected %@ as the capital of %@.", capital, state];
NSString *Message =[[NSString alloc]initWithFormat:lblMsg];

UIAlertView *alert =[[UIAlertView alloc]initWithTitle:Title message:Message delegate:@"" cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

if (StateRow == CapitalRow) {
    lblMsg = @"You are correct!";
    UIColor *myColor;
    switch ([not sure what to use here]) {
        case 0:
        myColor = [UIColor greenColor];
            break;
        default:
            break;
    }
}
else {
    lblMsg = @"Incorrect";
    UIColor *myColor;
    switch ([not sure what to use here]) {
        case 0:
        myColor = [UIColor redColor];
            break;        
        default:
            break;
    }
}

}

If anyone can offer any sort of help on this, I would appreciate it greatly! Thank you!

user1678809
  • 1
  • 1
  • 1
  • You need to set an exception breakpoint so you can see where the exception is being raised. Look at the answers to this question: http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 – rob mayoff Oct 15 '12 at 03:02
  • Ah, ok! The exception came up at NSString *Message, specifically at lblMsg. It says "Format string is not a string literal (potentially insecure)". I had this same yellow error on my other button, but it never formed an exception. – user1678809 Oct 15 '12 at 19:53

1 Answers1

0

Two things to check:

  • Where is lblMsg declared or assigned a value?
  • Are you sure capital and state are not nil from the calls to [arrayCapital objectAtIndex]?

Specifically: the NSString documentation states "Important: Raises an NSInvalidArgumentException if format is nil." for initWithFormat.

Smith
  • 361
  • 1
  • 10
  • I did declare lblMsg as an NSString, it gets its value from the if statement. I don't believe they are nil, like I said, it worked fine with hardcoded arrays. Is that something that happens when I release the arrays on viewDidUnload? – user1678809 Oct 15 '12 at 19:37