0

My app throws an NSInvalidArgumentException. I am able to step through the code and the exception appears to be thrown at this code snippet:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Rooms"];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rooms" inManagedObjectContext:managedObjectContext];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"roomName"]];

I am not good at reading the stack which follows:

2015-07-29 20:30:22.423 HomeSense[55434:2541818] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(
  0   CoreFoundation                      0x00000001120cfc65 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x0000000111d68bb7 objc_exception_throw + 45
  2   CoreFoundation                      0x0000000111f94478 -[__NSPlaceholderArray initWithObjects:count:] + 360
  3   CoreFoundation                      0x0000000112007b4f +[NSArray arrayWithObject:] + 47
  4   HomeSense                           0x000000010fe0b78a -[HSPaintLogViewController viewDidLoad] + 666
  5   UIKit                               0x00000001107271d0 -[UIViewController loadViewIfRequired] + 738
  6   UIKit                               0x00000001107273ce -[UIViewController view] + 27
  7   UIKit                               0x0000000110cb432d -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
  8   UIKit                               0x0000000110701d69 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
  9   UIKit                               0x0000000110733248 -[UIViewController _presentViewController:withAnimationController:completion:] + 1761
  10  UIKit                               0x00000001107356c1 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
  11  UIKit                               0x00000001107355e5 -[UIViewController presentViewController:animated:completion:] + 229
  12  UIKit                               0x00000001105f7d62 -[UIApplication sendAction:to:from:forEvent:] + 75
  13  UIKit                               0x00000001105f7d62 -[UIApplication sendAction:to:from:forEvent:] + 75
  14  UIKit                               0x000000011070950a -[UIControl _sendActionsForEvents:withEvent:] + 467
  15  UIKit                               0x00000001107088d9 -[UIControl touchesEnded:withEvent:] + 522
  16  UIKit                               0x0000000110644958 -[UIWindow _sendTouchesForEvent:] + 735
  17  UIKit                               0x0000000110645282 -[UIWindow sendEvent:] + 682
  18  UIKit                               0x000000011060b541 -[UIApplication sendEvent:] + 246
  19  UIKit                               0x0000000110618cdc _UIApplicationHandleEventFromQueueEvent + 18265
  20  UIKit                               0x00000001105f359c _UIApplicationHandleEventQueue + 2066
  21  CoreFoundation                      0x0000000112003431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  22  CoreFoundation                      0x0000000111ff92fd __CFRunLoopDoSources0 + 269
  23  CoreFoundation                      0x0000000111ff8934 __CFRunLoopRun + 868
  24  CoreFoundation                      0x0000000111ff8366 CFRunLoopRunSpecific + 470
  25  GraphicsServices                    0x0000000114ee7a3e GSEventRunModal + 161
  26  UIKit                               0x00000001105f68c0 UIApplicationMain + 1282
  27  HomeSense                           0x000000010fe06d7f main + 111
  28  libdyld.dylib                       0x0000000113390145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

This particular functionality was working fine until now. I am totally clueless as to what is going on here, so any help would be greatly appreciated.

zpasternack
  • 17,838
  • 2
  • 63
  • 81
soundChaser
  • 115
  • 1
  • 1
  • 8

2 Answers2

1

You are trying to store nil value into the array.Before store check the nil value as.

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rooms" inManagedObjectContext:managedObjectContext];
    request.resultType = NSDictionaryResultType;
    if([[entity propertiesByName] objectForKey:@"roomName"]!=nil){
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"roomName"]];
    }
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
0

Check your object before adding it to your array... You're probably getting an error about trying to insert a nil object at location 0.

reason: '* -[NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' ***

You can also use Breakpoint in your code to narrow down where the crash happens.

Pepeng Hapon
  • 357
  • 1
  • 17