Exception with insertObject:atIndex: on iOS6
The reason for the crash is that the object you are trying to insert is nil
. This means it is not properly instantiated. This in turn means something has gone awry before you reached that exception.
Could you post the code that alloc
ed and init
ialized the object you were trying to insert?
In order to find the relevant line of code, please try the following: Go to the "Exception" tab in your Xcode project:
Then click the "+" button (at the bottom of the page) and select "Add Exception Breakppoint ...". Leave all settings to their defaults and click "Done".
If you rerun your project it should now stop at the relevant line of code before the exception is thrown. Then you can move up the call-stack and identify from where in your code you called the library function that is responsible for this behavior. Then try to see if all objects are correctly initialized at this point.
[__NSArrayM insertObject:atIndex:]: object cannot be nil
Make sure you have initialized your array called phoneno before adding any object into it...
NSMutableArray *phoneno=[[NSMutableArray alloc]init];
and stop adding any nil object into it...
if(str) //or if(str != nil) or if(str.length>0)
{
//add the str into phoneno array.
}
else
{
//add the @""
}
NSArrayM insertObject:atIndex:]: object cannot be nil'
You haven't listed enough code in your question to definitely show where you're making an error.
Look in your code for any lines that say "insertObject:atIndex:
". The object you're inserting is apparently nil.
If you can't find this line, add a symbolic breakpoint on the symbol "[NSArray insertObject:atIndex:]
" (click the link to see specific instructions in an answer of a closely related question to yours) and see if you can break on it right before the crash happens.
crash only with iPad (simulator or device) - [__NSArrayM insertObject:atIndex:]
I deleted my earlier answer and I am posting this new answer instead.
Look at the stack trace. The problem is due to calling:
_bannerView.delegate = self;
This results in a chain of calls that leads to the loadView
method of BannerViewController
being called while still in the middle of the initWithContentViewController:
being executed. But at this point, the _contentController
ivar isn't set yet.
Given that setting the delegate kicks off ad loads and the need for a view, the best solution is to wait and set the _bannerView.delegate
in the viewDidLoad
method.
Another solution is to reorder the calls in the initWithContentViewController:
method to:
- (instancetype)initWithContentViewController:(UIViewController *)contentController {
self = [super init];
if (self != nil) {
// This must be called before you set the delegate on _bannerView
_contentController = contentController;
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_bannerView = [[ADBannerView alloc] init];
}
_bannerView.delegate = self;
}
return self;
}
Yet another solution would be to get rid of the loadView
implementation and put all of that code (minus creating the content view) at the start of viewDidLoad
.
[__NSArrayM insertObject:atIndex:]: object cannot be nil - how determine where is the error?
A good way to check where your code is crashing is:
1) goTo the breakpoint tab in Xcode.
2) click on the '+" button at the bottom.
3) Add Exception Breakpoint
a) In the break tab select both:
i) on Throw
ii) on Catch
and build and r
un.
4) These breakpoints will give you exactly where your app is crashing 90% of the times.
Hope this helps you in someway. :)
Related Topics
Detect Moment When Newline Starts in Uitextview
How to Take Screenshot of Portion of Uiview
Scanning Barcode or Qr Code in Swift 3.0 Using Avfoundation
How to Determine If the Sim/Phone Number Has Changed
Workaround for Rounded Corners of Grouped Uitableview, iOS7
Skspritenode Position in Universal Game
How to Create Custom View Programmatically in Swift Having Controls Text Field, Button etc
Where to Place a .Txt File and Read from It in a iOS Project
Wait for Swift Animation to Complete Before Executing Code
Swift: Equivalent Objective-C Runtime Class
Titletextattributes Uiappearance Font in iOS 7
iOS 10 Heading Arrow for Mkuserlocation Dot
How to Disable Custom Keyboards (Ios8) for My App