Below is the code for fetching data in sqlite db:
-(id) init {
if ((self = [super init])) {
NSString *sqLiteDb = [[NSBundle mainBundle]pathForResource:@"CourseFindr" ofType:@"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &_db) != SQLITE_OK){
NSLog(@"Failed to open database!");
}
}
return self;
}
-(void)dealloc {
sqlite3_close(_db);
}
- (NSArray *)job {
NSMutableArray *retrieve = [[NSMutableArray alloc] init];
NSString *query = @"SELECT jID, jName, jDesc, jEarnings, jTags FROM jobs";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_db, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int _jID = sqlite3_column_int(statement, 0);
char *jNameChars = (char *) sqlite3_column_text(statement,1);
char *jDescChars = (char *) sqlite3_column_text(statement, 2);
char *jEarningsChars = (char *) sqlite3_column_text (statement, 3);
char *jTagsChars = (char *) sqlite3_column_text(statement, 4);
NSString *_jName =[[NSString alloc]initWithUTF8String:jNameChars];
NSString *_jDesc = [[NSString alloc]initWithUTF8String:jDescChars];
NSString *_jEarnings = [[NSString alloc]initWithUTF8String:jEarningsChars];
NSString *_jTags = [[NSString alloc]initWithUTF8String:jTagsChars];
Jobs *jobs = [[Jobs alloc]
initWithJID:_jID
jName:_jName
jDesc:_jDesc
jEarnings:_jEarnings
jTags:_jTags];
[retrieve addObject:jobs];
}
sqlite3_finalize(statement);
}
return retrieve;
}
But it keeps getting this crash:
2014-01-09 21:12:39.565 CourseFindr[4595:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x018255e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x015a88b6 objc_exception_throw + 44
2 CoreFoundation 0x018253bb +[NSException raise:format:] + 139
3 Foundation 0x0120fb49 -[NSString initWithUTF8String:] + 89
4 CourseFindr 0x000036b5 -[CourseFindrDB jobs] + 645
5 libdyld.dylib 0x01e61725 start + 0
6 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I'm not sure if it's because of my libsqlite3.dylib or is it not compatible with my database? My database is named 'CourseFindr.sqlite' table is
JOBS Table
-jID PK
-jName
-jDesc
-jEarnings
-jTags
Also it keeps redirecting to main.m when it crashes.
I've done this before, but there were no errors. This time I'm trying it without tutorials, but it seems I'm missing something. :/
The crash log:
2014-01-09 21:12:39.565 CourseFindr[4595:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithUTF8String:]: NULL cString'
is telling you that you are passing a NULL pointer to initWithUTF8String
. This is the reason of the crash and is expected behaviour according to Apple docs:
initWithUTF8String:
…
Important: Raises an exception if bytes is NULL.
So, inspect what you are passing into the initWithUTF8String
method and add guards against passing NULL. Something like:
char *jNameChars = (char *) sqlite3_column_text(statement,1);
...
NSString *_jName = jNameChars?[[NSString alloc]initWithUTF8String:jNameChars]:@"";
You can find the solution in the first line of the exception.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'
Which means that you are passing a null string to the initWithUTF8String method.
NSLog all strings you are passing. because you are passing a null to initWithUTF8String
User contributions licensed under CC BY-SA 3.0