0

I am populating a pickerView from an array. It loads into the page okay. However, when you scroll through the choices of the pickerView at the 11th choice, it crashes with a [NSNull rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x19fc50490 error.

I really have no idea what to try next and would be grateful for any suggestions:

Here is my code:

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
    return _actnames.count;//THis logs as 30 as there are 30 possibilities
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
NSLog(@"before crash");  //last line to display before crash
          return _actnames[row];
    }

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selectedActName = ([_actnames objectAtIndex:row]);
}

The last method is the one where it appears to crash. As mention, it crashes when you select the 11th item, though there are 30.

Thanks for any ideas.

Update:

This is method that pulls actnames from core data:

- (id) getActNames{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Activities"];
    fetchRequest.resultType = NSDictionaryResultType;
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
    [fetchRequest setSortDescriptors:@[sort]];
    NSError *error      = nil;
    self.managedObjectContext = [Model sharedInstance].managedObjectContext;

    NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest
                                                                   error:&error];
    NSMutableArray * actnames = [[results valueForKey:@"actname"] mutableCopy];
    return actnames;
}
user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Your code looks OK to me. To debug the issue, I'd set an exception breakpoint in Xcode, and see where exactly does the error occur. This answer explains how to set an exception breakpoint : http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode – TotoroTotoro Oct 18 '16 at 21:50
  • Did you parse some JSON to fill your `_actnames` array? It has an `NSNull` reference in it. – rmaddy Oct 18 '16 at 21:50
  • Update your question with how you create and populate `_actnames`. – rmaddy Oct 18 '16 at 21:51
  • It sounds like `actnames[11]` is `[NSNull null]`. You need to be prepared for that possibility, I guess. – matt Oct 18 '16 at 22:04
  • When I log out the array as a comma delimited list, I see that there is a NULL, not where it crashes, but a few items later. {"item1","item2",..."item14","0(NSNull)","item15"} Perhaps this is causing crash. How would I work around a null value in the array? – user6631314 Oct 18 '16 at 22:06
  • Ok, that was it. Fixed bug by adding line [actnames removeObjectIdenticalTo:[NSNull null]]; Thanks everyone! – user6631314 Oct 18 '16 at 22:11

0 Answers0