2012-03-08 2 views
0

私は、データベースからの情報を表示するために、タブコントロール、データベース、およびセグメント化されたコントロールをナビゲーションコントロールに(プログラムで)作成しています。Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'コンソールウィンドウで。NSInvalidArgumentException:[__NSArrayM insertObject:atIndex:]:オブジェクトはゼロにすることはできません

TableViewAppDelegate。

#import "SegmentsController.h" 
#import "TableViewAppDelegate.h" 
#import "RootViewController.h" 
#import "AtoZHomePageViewController.h" 
#import "CollectionRecipe.h" 
#import "NSArray+PerformSelector.h" 

@interface TableViewAppDelegate() 

- (NSArray *)segmentViewControllers; 
- (void)firstUserExperience; 
@end 


@implementation TableViewAppDelegate 

@synthesize window; 
@synthesize navigationController; 
@synthesize tabbarController; 
@synthesize recipes; 
@synthesize segmentsController, segmentedControl; 


#pragma mark - 
#pragma mark Application lifecycle 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    NSArray * viewControllers = [self segmentViewControllers]; 
    // UINavigationController * navigationController = [[[UINavigationController alloc] init] autorelease]; 
    self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers]; 

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]]; 
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 

    [self.segmentedControl addTarget:self.segmentsController 
           action:@selector(indexDidChangeForSegmentedControl:) 
        forControlEvents:UIControlEventValueChanged]; 


    databaseName = @"RecipeDatabase.sql"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 

    // Execute the "checkAndCreateDatabase" function 
    [self checkAndCreateDatabase]; 

    // Query the database for all animal records and construct the "animals" array 
    [self readRecipesFromDatabase]; 

    // Configure and show the window 
    [self firstUserExperience]; 
    [window addSubview:[tabbarController view]]; 
    [window addSubview:[navigationController view]]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 

#pragma mark - 
#pragma mark Segment Content 

- (NSArray *)segmentViewControllers { 
    UIViewController * AtoZRecipe  = [[AtoZHomePage alloc] initWithNibName:@"AtoZRecipeController" bundle:nil]; 
    UIViewController * RecipesCollection = [[CollectionRecipe alloc] initWithNibName:@"RecipeCollection" bundle:nil]; 

    NSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil]; 
    [AtoZRecipe release]; [RecipesCollection release]; 

    return viewControllers; 
} 

- (void)firstUserExperience { 
    self.segmentedControl.selectedSegmentIndex = 0; 
    [self.segmentsController indexDidChangeForSegmentedControl:self.segmentedControl]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application { 
    // Save data if appropriate 
} 
-(void) checkAndCreateDatabase{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 


} 

-(void) readRecipesFromDatabase { 
    // Setup the database object 
    sqlite3 *database; 

    // Init the animals Array 
    recipes = [[NSMutableArray alloc] init]; 

    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = "select * from recipe order by name"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 
       NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *aAuthor=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)]; 
       NSString *aThumbnail=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)]; 
       NSString *aPre_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,5)]; 
       NSString *aBake_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,6)]; 
       NSString *aTota_ltime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; 
       NSString *alarge_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,8)];      
       NSString *asmall_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)]; 
       NSString *asummary=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)]; 
       NSString *aServe_size=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,11)]; 
       // Create a new recipe object with the data from the database 
       AtoZHomePage *recipe=[[AtoZHomePage alloc] initWithName:aName author:aAuthor img_thumbnail:aThumbnail pre_Time:aPre_time bake_Time:aBake_time total_time:aTota_ltime large_Img:alarge_image small_Img:asmall_image summary:asummary serve_size:aServe_size]; 
        // Add the recipe object to the recipes Array 
       [recipes addObject:recipe]; 

       [recipe release]; 
      } 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database);  
} 



#pragma mark - 
#pragma mark Memory management 

- (void)dealloc { 
    self.segmentedControl = nil; 
    self.segmentsController = nil; 
    [recipes release]; 
    [tabbarController release]; 
    [navigationController release]; 
    [window release]; 
    [super dealloc]; 
} 

@end 

この行は、私は関連の記事のほとんどを見たようNSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil];曖昧であると私はそれnilを削除する場合センチネルを逃すと私は適切なView Controllerを呼んでいる私に従ってされます。 :)事前のおかげで負荷...このバグをオフに取り除くために私を助けて行ってください:)

+2

'[recipes addObject:recipe];'この行は一度クラッシュする可能性があります。ここにブレークポイントを追加してください。どんな場合でも、あなたの場所にたくさんのブレークポイントを追加し、どの関数がうまくいくかを確認してください。 – Shubhank

+3

...または例外ブレークポイントを追加し、問題のある行を確認してください。ブレークポイントナビゲータに加えて、左下の記号に例外ブレークポイントを追加します。 – jrturton

+2

すべてのメソッドの冒頭にブレークポイントを配置すると、アプリがどこで止まるかがわかります。これは修正方法を教えてくれます。あなたのコードが非常に長いことを考慮に入れて、人々は多くのコードと手がかりを見てすぐにあなたの質問を無視するつもりです...あなたの答えをより速く得るためのアドバイスだけ! –

答えて

3

を実際にあなたがあなたのNSInvalidArgumentException

この1

を試してみてくださいを取得した理由だ配列にメモリを割り当てるありません
NSArray * viewControllers = [NSArray arrayWithArray:[self segmentViewControllers]]; 
+0

私はNSArrayの代わりに上記のコードを試してみました* viewControllers = [self segmentViewControllers];まだ同じエラー:( – iMeMyself

関連する問題