how to remove error “[NSString stringWithUTF8String:]: NULL cString' ”?

匿名 (未验证) 提交于 2019-12-03 02:51:02

问题:

I am making an sqlite3 test3.sql database, making table as

CREATE TABLE rest (name VARCHAR(100), price VARCHAR(100)); 

table is successfully created but now when I am using a method for inserting and retrieving data, then getting error,,,

The error is on line

NSString *str = [NSString stringWithUTF8String:                  (char *)sqlite3_column_text(compiledStatement, 1)]; 

the error is +[NSString stringWithUTF8String:]: NULL cString'

What is this error, how can I remove it?

回答1:

Instead of just having

... = [NSString stringWithUTF8String:        (char *)sqlite3_column_text(compiledStatement, 15)] 

You want to use something like

... = ((char *)sqlite3_column_text(compiledStatement, 15)) ?        [NSString stringWithUTF8String:        (char *)sqlite3_column_text(compiledStatement, 15)] : nil; 


回答2:

After using the tenary operator to get the values from the db you have to check if the value is nil. Otherwise the program will still crash.

NSString *retrievedString = ((char *)sqlite3_column_text(statement, 15)) ?    [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 15)] :    nil;  if(retrievedString == nil){   [allSteps addObject:@""]; } else {   [allSteps addObject:retrievedString]; } 


回答3:

It means that sqlite3_column_text returns NULL. It returns NULL when the column is NULL.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!