2

I'm building an iOS app using Storyboards.I am facing an issue in my sqlite database i'm getting the field value here

NSLog(@"selfuserid: %@",selfuserid);

but my code goes again to the below code.

NSString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];

here is my code:

-(NSArray *) getRecords:(NSString*) filePath where:(NSString *)whereStmt
{
NSMutableArray * students =[[NSMutableArray alloc] init];
sqlite3* db = NULL;
sqlite3_stmt* stmt =NULL;
int rc=0;
rc = sqlite3_open_v2([filePath UTF8String], &db, SQLITE_OPEN_READONLY , NULL);
if (SQLITE_OK != rc)
{
    sqlite3_close(db);
    NSLog(@"Failed to open db connection");
}
else
{
    NSString  * query = @"SELECT * from students";
    if(whereStmt)
    {
        query = [query stringByAppendingFormat:@" WHERE %@",whereStmt];
    }
    rc =sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL);
    if(rc == SQLITE_OK)
    {
        while (sqlite3_step(stmt) == SQLITE_ROW) //get each row in loop
        {
NString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];//here the code comes again
NSString * profilepic =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString * username =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString * selectedsports =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
     NSDictionary *student =[NSDictionary dictionaryWithObjectsAndKeys:selfuserid,@"selfuserid",profilepic,@"profilepic",username,@"username",selectedsports,@"selectedsports", nil];
[students addObject:student];
NSLog(@"selfuserid: %@",selfuserid);i'm getting selfuserid here but code goes again to the above stm
NSLog(@"pic: %@",profilepic);
}
NSLog(@"Done");
sqlite3_finalize(stmt);
}
else
{
NSLog(@"Failed to prepare statement with rc:%d",rc);
}
sqlite3_close(db);
}
return students;
}

I'm getting this exception:

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'

    *** First throw call stack:
    (
    0   CoreFoundation                      0x000000010450ef35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001041a7bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010450ee6d +[NSException raise:format:] + 205
    3   Foundation                          0x0000000103d0c464 +[NSString stringWithUTF8String:] + 78
    4   Playo                               0x0000000101a038f9 -[Play getRecords:where:] + 617
    5   Playo                               0x0000000101a06697 -[Play tableView:didSelectRowAtIndexPath:] + 231
    6   UIKit                               0x0000000104c26393 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
    7   UIKit                               0x0000000104c264d4 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    8   UIKit                               0x0000000104b61331 _applyBlockToCFArrayCopiedToStack + 314
    9   UIKit                               0x0000000104b611ab _afterCACommitHandler + 516
    10  CoreFoundation                      0x0000000104443dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    11  CoreFoundation                      0x0000000104443d20 __CFRunLoopDoObservers + 368
    12  CoreFoundation                      0x0000000104439b53 __CFRunLoopRun + 1123
    13  CoreFoundation                      0x0000000104439486 CFRunLoopRunSpecific + 470
    14  GraphicsServices                    0x00000001063299f0 GSEventRunModal + 161
    15  UIKit                               0x0000000104b3e420 UIApplicationMain + 1282
    16  Playo                               0x0000000101a34813 main + 115
    17  libdyld.dylib                       0x0000000105f47145 start + 1
   )
   libc++abi.dylib: terminating with uncaught exception of type NSException
0

2 Answers 2

6

stringWithFormat does not crash if value is null

 NSString* str=[NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(statement, 0)];
Sign up to request clarification or add additional context in comments.

2 Comments

I know this is two years ago, but is this functionally equiv to stringWithUTF8String? i.e.: UTF8 equiv? I am getting null crash too and want to use this if it is safe and does the same thing.
it prevent crash, but still i m getting null value.
1

Check these lines:

NString * selfuserid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];//here the code comes again
NSString * profilepic =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString * username =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString * selectedsports =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];

What happens here that one of the above field is return NULL string. As I guess database design for these fields constrains has property that allow to insert NULL data to these fields.

So you have to check that if any one of these field returns NULL string then appropriately store an empty string.

Do the following for this:

Create one function to validate NULL strings:

- (NSString *) validateNilString:(NSString *)strValue {

    NSString *returnString = @"";

    @try {
        if (!strValue)
            return returnString;

        if ([strValue isKindOfClass:[NSNull class]])
            return returnString;

        if ([strValue isEqualToString:@"<nil>"])
            return returnString;

        if ([strValue isEqualToString:@"<null>"])
            return returnString;

        if ([strValue isEqualToString:@"NULL"])
            return returnString;

        if ([strValue isEqualToString:@"nil"])
            return returnString;

        if ([strValue isEqualToString:@"(null)"])
            return returnString;

        return strValue;
    }
    @catch (NSException *exception) {
        NSLog(@"Exception :%@",exception);
        return returnString;
    }
}

Check every string using this function for not null value:

NSString * selfuserid = [self validateNilString:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)]];
//here the code comes again
NSString * profilepic = [self validateNilString:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)]];
NSString * username = [self validateNilString:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)]];
NSString * selectedsports = [self validateNilString:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.