Am new to IOS programming and building my concepts of Core data. Am following guidelines in apple documentation. i have a table view controller and a mutable array, StocksArray, as one of its property. I have another view, DetailedController, which am using to get user input for the entity properties of core data model. Am passing a NSManagedObject into the DetailedController object and using a delegate for updating the mutable array, StocksArray. On compiling, am getting an error—Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘* -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0′. Will really appreciate if someone can help me.
#import"StockEntity.h"
@protocol DetailedControllerdelegate <NSObject>
-(void)addstockinarray:(StockEntity*)anObject;
@end
@interface DetailedController : UIViewController {
IBOutlet UITextField* stocksymboltextfield;
IBOutlet UITextField* stocknametextfield;
//NSManagedObjectContext* context2;
StockEntity* asinglestock;
}
@property (nonatomic, retain) IBOutlet UITextField* stocksymboltextfield;
@property (nonatomic, retain) IBOutlet UITextField* stocknametextfield;
//@property(nonatomic, retain) NSManagedObjectContext* context2;
@property(nonatomic, retain) StockEntity* asinglestock;
@property (nonatomic, assign) id <DetailedControllerdelegate> delegate;
@implementation DetailedController
@synthesize stocksymboltextfield, stocknametextfield,asinglestock, delegate;//context2;
-(void)onDone:(id)sender{
[asinglestock setStockName:self.stocknametextfield.text];
[asinglestock setStockSymbol:self.stocksymboltextfield.text];
NSLog(@"lets s see: %@", [asinglestock StockName]);
NSError *error = nil;
if (![[asinglestock managedObjectContext] save:&error] ) {
//handle the error
}
if ([delegate respondsToSelector:@selector(addstockinarray:)]) {
NSLog(@"before delegate");
[delegate addstockinarray:asinglestock];
NSLog(@"am in delegate");
}
[self.navigationController popViewControllerAnimated:YES];
}
And this is the implementation of the table view
@implementation maintable
@synthesize stocksArray, managedObjectContext, addButton;
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
self.stocksArray =[[NSMutableArray alloc]init];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//self.stocksArray =[[NSMutableArray alloc]init];
self.title = @"Stocks";
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStock)];
//addButton.enabled = NO;
self.navigationItem.rightBarButtonItem = addButton;
}
-(void) addStock{
DetailedController *detail = [[DetailedController alloc]initWithNibName:NSStringFromClass([DetailedController class]) bundle:[NSBundle mainBundle]];
//detail.context2 = managedObjectContext;
StockEntity *stock = (StockEntity*)[NSEntityDescription insertNewObjectForEntityForName:@"StockEntity"
inManagedObjectContext:managedObjectContext];
detail.asinglestock = stock;
detail.delegate = self;
//[stocksArray insertObject:stock atIndex:0];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
-(void)addstockinarray:(StockEntity*)anObject{
if (anObject = NULL) {
NSLog(@"YESSS");
}
[self.stocksArray addObject:anObject];
NSLog(@"LETTTTTT: %i", [self.stocksArray count] );
}
- (void)viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
[super viewWillAppear:animated];
}
Try this to handle the crash,
Your issue is that you are trying to insert, nil value to the array. You should check why
anObjectis nil and fix it there. If I am correct,asinglestockis somehow nil and you are trying to insert that.