views:

595

answers:

2

I am new in iphone application developer i am using sqlite3 database and in app delegate i am wright following code and run properly we also find value from database to in my aplication,

but immediately the application is going to crass why this is occurs i am not understand.

code is given bellow

        -(void)Data
      {
   databaseName = @"dataa.sqlite";

        NSArray *documentPaths =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDir = [documentPaths objectAtIndex:0];
     databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

    [self checkAndCreateDatabase];

    list1 = [[NSMutableArray alloc] init];  

    sqlite3 *database;
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
       if(detailStmt == nil)
        {
                    const char *sql = "Select * from Dataa";            
             if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) ==    SQLITE_OK)
            {               
                    //NSLog(@"Hiiiiiii");
                //sqlite3_bind_text(detailStmt, 1, [t1 UTF8String], -1,    SQLITE_TRANSIENT);
                //sqlite3_bind_text(detailStmt, 2, [t2 UTF8String], -2, SQLITE_TRANSIENT);
                  //sqlite3_bind_int(detailStmt, 3, t3);

                   while(sqlite3_step(detailStmt) == SQLITE_ROW) 
                 {
                    //NSLog(@"Helllloooooo");




                     NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];
                //NSString *fame= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 1)];
                //NSString *cinemax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 2)];
                //NSString *big= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 3)];

                //pvr1 = pvr;
                item1=item;
                //NSLog(@"%@",item1);

                data = [[NSMutableArray alloc] init];   


                list *animal=[[list alloc] initWithName:item1];

                // Add the animal object to the animals Array
                [list1 addObject:animal];
                //[list1 addObject:item];


            }
            sqlite3_reset(detailStmt);
        }
        sqlite3_finalize(detailStmt);
        //  sqlite3_clear_bindings(detailStmt);
    }
}
detailStmt = nil;
sqlite3_close(database);

}

when we see console they show the following error giving bellow

    2010-03-09 10:02:40.262 SanjeevKapoor[430:20b] *** Terminating app due to uncaught     exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]:    NULL cString'

when we see debugger they show error in following line

          NSString *item= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];

I am not able to solve that problum plz help me.

+1  A: 

Sqlite can store NULL objects, which are not same as empty strings. In your case you have extracted value from base, and result was NULL. But [NSString stringWithUTF8String:] method can't use NULL as argument, so it raise error. You should check extracted value:

char * str = (char*)sqlite3_column_text(statement, i);
if (str){
    item = [NSString stringWithUTF8String:str];
}
else{
    // handle case when object is NULL, for example set result to empty string:  
    item = @"";
}
Vladimir
A: 

You can use NSString stringWithFormat:@"%s" - this also gracefully handles NULL values

Lutz