2012-03-08 101 views
0

我正在用導航控件(編程方式)創建一個帶有標籤欄應用程序,數據庫和分段控件的項目,以便顯示來自數據庫的信息,並且我得到了帶有Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'的SIGABRT在控制檯窗口中。NSInvalidArgumentException:[__NSArrayM insertObject:atIndex:]:object can not be nil

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];,因爲我看到最相關的帖子,如果我刪除零它會根據我,我有所謂適當的視圖控制器缺少定點和。 請你幫我擺脫掉這個bug ...在此先感謝負荷:) :)

+2

'[食譜ADDOBJECT:配方]'此行可能是一次崩潰..在這裏添加一個斷點。在任何情況下,添加斷點在很多地方S的你是男女合校,並檢查其功能成功地通過.. – Shubhank 2012-03-08 07:34:23

+3

...或者添加一個異常斷點並查看問題的確切位置。斷點導航器,加上左下角的符號,添加異常斷點。 – jrturton 2012-03-08 08:41:35

+2

在所有方法的開始處放置一個斷點,所有方法都會告訴我們您的應用程序停止的位置,這將告訴我們如何解決該問題。考慮到你的代碼很長,只要他們看到如此多的代碼,並且根本沒有線索,人們就會忽略你的問題......只是一個建議,讓你的答案更快! – 2012-03-08 08:34:04

回答

3

其實你不分配的內存陣列是你得到NSInvalidArgumentException

試試這個

的原因
NSArray * viewControllers = [NSArray arrayWithArray:[self segmentViewControllers]]; 
+0

我試過上面的代碼而不是NSArray * viewControllers = [self segmentViewControllers];還是一樣的錯誤:( – iMeMyself 2012-03-09 10:38:28

相關問題