Xcode - NSInvalidArgumentException',原因:'*** + [NSString stringWithUTF8String:]:NULL cString'

时间:2014-08-07 10:01:54

标签: objective-c xcode5

我有下面的代码,应该用我的sqlite文件中的信息填充TableViewController,它让我添加正常,我可以查看文件和信息,但我收到上面的错误信息,并失败惨遭修复......

-(NSMutableArray *) stockList
{

NSString *filePath = [self getWritableDBPath];

if(sqlite3_open([filePath UTF8String], &db) == SQLITE_OK)
{

    const char *sql = "Select Description, Quantity from StockTable";
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with query: %s", sqlite3_errmsg(db));
    }
    else
    {
        while (sqlite3_step(sqlStatement)==SQLITE_ROW)
        {
            Stock * stock = [[Stock alloc] init];

            stock.desc = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,     1)];
            stock.qty = [NSString stringWithUTF8String:(char *)     sqlite3_column_text(sqlStatement, 2)];

            [thestock addObject:stock];
            stock = nil;
        }
    }
    sqlite3_finalize(sqlStatement);
}
sqlite3_close(db);
return thestock;
}

感谢您的帮助,目前正在谷歌上搜索..

连接字符串如下所述:(原因是它导致LINK错误并指出MyDB在两个视图中都是重复的)

的TableView:

NSString * MyDB2=@"StockDatabase.db";

AddingView:

NSString * MyDB=@"StockDatabase.db";

2 个答案:

答案 0 :(得分:3)

您从数据库中提取的一列(或两列)是NULL

stock.desc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement, 1)];
stock.qty = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement, 2)];

通过以下方式防范:

const char *desc = sqlite3_column_text(sqlStatement, 1);
if (desc)
    stock.desk = @(desc);
const char *qty = sqlite3_column_text(sqlStatement, 2);
if (qty)
    stock.qty = @(qty);

答案 1 :(得分:0)

我也遇到了这个错误。我如何解决它是通过将第一列文本索引设置为" 0"而不是" 1"。错误就消失了。

char *charPrice = (char*) sqlite3_column_text(stmt, 0);
NSString *price = [NSString stringWithUTF8String:charPrice];

char *charName = (char*) sqlite3_column_text(stmt, 1);
NSString *name = [NSString stringWithUTF8String:charName];