Randi
Randi

Reputation: 717

[NSPlaceholderMutableString initWithString:]: nil argument exception coming after casting the UITextField

I have a code like this.

else if ([controlIn isKindOfClass:UITextField.class]) {
    UITextField *cast = (UITextField *)controlIn;
    if (cast.inputView != NULL) {
        cast.text = [self setResolvedValue:cast metrixUIViewControllerIn:metrixUIViewControllerIn valueIn:valueIn];
    } else {
        cast.text = valueIn;
    }

But in this if (cast.inputView != NULL) line I am getting an exception

"[NSPlaceholderMutableString initWithString:]: nil argument"

My cast is not nil. But whats the reason to get this error. Please help me. Thanks

UPDATE

+ (NSString*) setResolvedValue:(UIView *)viewIn metrixUIViewControllerIn:(MetrixUIViewController *)metrixUIViewControllerIn valueIn:(NSString *)valueIn {
NSMutableString *resolvedValue = [NSMutableString stringWithString:valueIn];
if (![MetrixStringHelper isNullOrEmpty:valueIn] && [viewIn.inputView isKindOfClass:[UIPickerView class]]) {
    MetrixColumnDef *columnDef = [MetrixControlAssistant getColumnDefAssociatedToPicker:metrixUIViewControllerIn picker:(UIPickerView *)viewIn.inputView];
    NSString *uniqueName = [MetrixControlAssistant getUniqueNameAssociatedToPicker:metrixUIViewControllerIn picker:(UIPickerView *)viewIn.inputView];
    NSMutableArray *dataSet = [metrixUIViewControllerIn.pickerData objectForKey:uniqueName];
    for (NSDictionary *dictionary in dataSet) {
        if ([MetrixStringHelper value:valueIn isEqualTo:[dictionary objectForKey:columnDef.lookupDef.valueColumn]]) {
            resolvedValue = [dictionary objectForKey:columnDef.lookupDef.displayColumn];
            break;
        }
    }
}
return resolvedValue;

}

Upvotes: 1

Views: 1993

Answers (1)

trungduc
trungduc

Reputation: 12154

The problem is because of valueIn is nil when you initialize resolvedValue. Check and make sure valueIn isn't nil before using it to initialize resolvedValue.

You should take a look at Apple document for stringWithString: method.

Raises an NSInvalidArgumentException if aString is nil.

Try to replace your method by the code below

+ (NSString*) setResolvedValue:(UIView *)viewIn metrixUIViewControllerIn:(MetrixUIViewController *)metrixUIViewControllerIn valueIn:(NSString *)valueIn {
  if ([MetrixStringHelper isNullOrEmpty:valueIn] || ![viewIn.inputView isKindOfClass:[UIPickerView class]]) {
    return @"";
  }

  NSMutableString *resolvedValue = [NSMutableString stringWithString:valueIn];
  MetrixColumnDef *columnDef = [MetrixControlAssistant getColumnDefAssociatedToPicker:metrixUIViewControllerIn picker:(UIPickerView *)viewIn.inputView];
  NSString *uniqueName = [MetrixControlAssistant getUniqueNameAssociatedToPicker:metrixUIViewControllerIn picker:(UIPickerView *)viewIn.inputView];
  NSMutableArray *dataSet = [metrixUIViewControllerIn.pickerData objectForKey:uniqueName];
  for (NSDictionary *dictionary in dataSet) {
    if ([MetrixStringHelper value:valueIn isEqualTo:[dictionary objectForKey:columnDef.lookupDef.valueColumn]]) {
      resolvedValue = [dictionary objectForKey:columnDef.lookupDef.displayColumn];
      break;
    }
  }
  return resolvedValue;
}

Upvotes: 1

Related Questions