Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 728
Chatar Veer Suthar Avatar asked Feb 11 '11 10:02

Chatar Veer Suthar


3 Answers

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;
like image 90
Marc Avatar answered Nov 19 '22 19:11

Marc


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];
}
like image 22
Alex Cio Avatar answered Nov 19 '22 21:11

Alex Cio


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

like image 3
Max Avatar answered Nov 19 '22 20:11

Max