1
votes

I get the following error when I try to run my program. Any reasons why I get it? I have tried for several hours now but I give up. THANKS!

Error:

2011-03-08 19:18:29.967 iphoneApp[2561:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithUTF8String:]: NULL cString'

- (AnswerDTO *)findAnswerById:(NSInteger)primary_key {
    AnswerDTO *dto = [[[AnswerDTO alloc] init] autorelease];

    sqlite3_stmt *statement = nil;
    const char *sql = "SELECT * FROM klb_answers WHERE id = 7;";

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
        sqlite3_bind_int(statement, 1, primary_key);

        NSInteger answerId = sqlite3_column_int(statement, ANSWER_ID);
        NSInteger answerQuestionId = sqlite3_column_int(statement, ANSWER_QUESTION_ID);
        NSString *answerDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, ANSWER_DESCRIPTION)];
        NSString *answerSerialNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, ANSWER_SERIAL_NUMBER)];
        NSInteger answerIsCorrect = sqlite3_column_int(statement, ANSWER_IS_CORRECT);

        dto.answerID = answerId;
        dto.questionID = answerQuestionId;
        dto.description = answerDescription;
        dto.serialNumber = answerSerialNumber;

        if (answerIsCorrect == 0) {
            dto.isCorrect = NO;
        } else {
            dto.isCorrect = YES;
        }
    } else {
        NSLog(@"Noe feil med statementet");
    }

    sqlite3_finalize(statement);

    return dto;
}
1
Only masochists use the SQLite C API in Objective-C. Use FMDB or CoreData instead.Dave DeLong

1 Answers

2
votes

It doesn't look like you are "running" your query. You prepared it and bound your params but you never ran sqlite3_step() to execute the query and get the response. Even if it is a single record or a scalar.

As a result, your column functions returning null values.