Search code examples
iosobjective-cinvalidargumentexception

App crashing on selecting media from library with exception that a nil element is being inserted into array


I have an old App which my company still provides legacy support for.

There's a strange error that's occurring for some of our clients but not on our end. It apparently happens when the user picks some image from ImagePickerController.

Exception

The exception says:

Fatal Exception: NSInvalidArgumentException

-[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

Stack trace

The stack trace is:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x2017b7ea0 __exceptionPreprocess
1  libobjc.A.dylib                0x200989a40 objc_exception_throw
2  CoreFoundation                 0x20172f470 _CFArgv
3  CoreFoundation                 0x2016a85f4 -[__NSPlaceholderArray initWithObjects:count:]
4  CoreFoundation                 0x2017b3600 __createArray
5  CoreFoundation                 0x2016b0584 +[NSArray arrayWithObject:]
6  MyAppName             0x102191378 -[PSImageViewController imagePickerController:didFinishPickingMediaWithInfo:] (PSImageViewController.m:221)
7  UIKitCore                      0x22e30e30c -[UIImagePickerController _imagePickerDidCompleteWithInfo:]
8  PhotoLibrary                   0x214add528 (Missing)
9  CameraUI                       0x21f38c728 -[CAMImagePickerCameraViewController _handleCapturedImagePickerPhotoWithCropOverlayOutput:]
10 AssetsLibraryServices          0x20f173814 __pl_dispatch_sync_block_invoke
11 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
12 libdispatch.dylib              0x20119f6b0 _dispatch_async_and_wait_invoke
13 libdispatch.dylib              0x2011f2484 _dispatch_client_callout
14 libdispatch.dylib              0x20119e9b4 _dispatch_main_queue_callback_4CF$VARIANT$mp
15 CoreFoundation                 0x201747dd0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                 0x201742c98 __CFRunLoopRun
17 CoreFoundation                 0x2017421cc CFRunLoopRunSpecific
18 GraphicsServices               0x2039b9584 GSEventRunModal
19 UIKitCore                      0x22e985054 UIApplicationMain
20 MyAppName             0x1020d60fc main (main.m:17)
21 libdyld.dylib                  0x201202bb4 start

Code

My code for ImagePickerController's delegate is:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if([info objectForKey:UIImagePickerControllerEditedImage])
    {
        self.image = [info valueForKey:UIImagePickerControllerEditedImage];
    }
    else
    {
        self.image = [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    [self.imgProperty setImage:self.image];
     [[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];
    [self dismissViewControllerAnimated:YES completion:nil];

}

As you can see, I am not actually doing anything here that might cause that exception. The code which opens the imagePicker is:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    CLS_LOG(@"Clicked Index %d",buttonIndex);

    if (actionSheet.tag == 1)
    {
        if (buttonIndex != actionSheet.cancelButtonIndex)
        {
            if (buttonIndex == actionSheet.firstOtherButtonIndex)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypeCamera];

            }
            else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1)
            {
                [self showCameraOfSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; return;
            }

        }
    }
}

The specific line in the trace PhotoLibrary 0x214add528 (Missing) is very curious. I think somehow the user doesn't have a PhotoLibrary or that somehow a wrong media source is chosen for Imagepicker. Or is it

[[PSDataPersistenceManager sharedManager] updateModificationStatusOfAppointments:[NSArray arrayWithObject:self.propertyObj.propertyToAppointment] toModificationStatus:[NSNumber numberWithBool:YES]];

which is causing the issue with self.propertyObj.propertyToAppointment somehow being null? I have checked, it can't be null. Or there would be more crashes on this screen. It is always valid in my test-runs.

How can I fix this crash?


Solution

  • The error is from line 221. You create an array with:

    [NSArray arrayWithObject:self.propertyObj.propertyToAppointment]
    

    The error is telling you that self.propertyObj.propertyToAppointment is nil.