Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:11:59+00:00 2026-06-15T02:11:59+00:00

Am new to IOS programming and building my concepts of Core data. Am following

  • 0

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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:12:00+00:00Added an answer on June 15, 2026 at 2:12 am

    Try this to handle the crash,

    - (void)addstockinarray:(StockEntity*)anObject{
    
      if (anObject) {
        [self.stocksArray addObject:anObject];
      } else {
        NSLog(@"Object is nil, do the error handling here");
      }
    
      NSLog(@"LETTTTTT: %i", [self.stocksArray count] );
    }
    

    Your issue is that you are trying to insert, nil value to the array. You should check why anObject is nil and fix it there. If I am correct, asinglestock is somehow nil and you are trying to insert that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to iOS programming, i read the view controller programming guide but
Im new to iOS programming and I'm trying to get the table view running
new to IOS OpenGL programming. I have worked through a few tutorials that talk
I'm fairly new to iOS programming, but have a basic understanding of it. I
I'm very new to IOS programming and i have doubt with NSThread. My problem
I am new to iOS programming, and I am having trouble with passing data
I'm new to iOS programming, I have a project going on and along with
I am very new to iOS Programming and have been learning for only a
I am following the Big Nerd Ranch iOS Programming Guide(3rd Edition), and have followed
I am new to IOS programming. So far I have been programming in android.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.