Terminate called throwning an exception - objective-c

I have make a piece of code for playing a video file. But when i build it i get the folowing error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
First throw call stack:
(0x1ed8022 0x11c0cd6 0x1e80a48 0x1e809b9 0xc6553b 0xc654c5 0x2be8 0x1ed9e99 0x32614e 0x3260e6 0x3ccade 0x3ccfa7 0x3cc266 0x34b3c0 0x34b5e6 0x331dc4 0x325634 0x17baef5 0x1eac195 0x1e10ff2 0x1e0f8da 0x1e0ed84 0x1e0ec9b 0x17b97d8 0x17b988a 0x323626 0x29b2 0x2925)
terminate called throwing an exception
I have tried a lot to fix it but nothing does work! Do you no a solution? And yes, i have implementated the mediaplayer framework!
I have coded the follwing code:
-(IBAction)playvideo {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"film" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[playercontroller release];
playercontroller = nil;
}

The problem is described in this line:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSURL initFileURLWithPath:]: nil string parameter'
Which is telling you that you are passing a nil into a parameter that doesn't allow a nil.
Steps to be sure that this is the problem:
-(IBAction)playvideo {
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"film" ofType:#"mp4"]
if (!videoPath) {
NSLog(#"Video path is nil. My bundle must be set up incorrectly");
return; // return early.
}
NSURL *url = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
// The rest of your implementation here.
}
This should at least help you to localise the problem.
As an aside - you should be using the URL based methods for getting resources instead of string based file-paths. But one thing at a time.

Related

NSPersistentStoreCoordinator addPersistentStoreWithType… causes exception

I'm trying to use Core Data on a project with code adapted from iOS Programming: The Big Nerd Ranch Guide (3rd ed). Opening the SQLite file is causing an exception, and no amount of documentation reading or search engine digging is helping me figure out what it means or how to avoid it.
The code in question is in the init method of a data store class, and reads as follows:
- (id)init
{
self = [super init];
if (self) {
// snip
NSURL *storeURL = [NSURL fileURLWithPath:[self itemArchivePath]];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] init];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
[NSException raise:#"Couldn't open the SQL file" format:#"Reason: %#", [error localizedDescription]];
}
// snip
}
return self;
}
The exception is happening on the addPersistentStoreWithType: line, so we're not making it to the exception in the if block. Here's what I'm being told:
2013-07-01 14:46:04.647 (app name)[5859:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: NSStoreModelVersionHashes)'
When I set a breakpoint on the faulty line, Xcode doesn't let me go inside the function call, so I'm not sure where NSStoreModelVersionHashes is coming from or how to avoid it being set to nil. I can po storeURL and it seems to be the proper URL. Resetting the iOS Simulator, using a different name for the SQL file, or other solutions I've found for issues which seem to be tangentally related hasn't seemed to help anything.
The persistent store coordinator needs the managed object model. It is usually created with
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
where managedObjectModel has previously been created with
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"<yourModelName>" withExtension:#"momd"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
Have a look at any of the Core Data sample code in the Apple Developer Library for examples
how to setup the Core Data stack correctly.

Error Thread 1: signal SIGBRT

I'm learning Objective C and I'm following examples from this book. However in some examples I keep getting this error! I followed the steps exactly, please help! I've already tried restoring the iOS simulator to its default settings and still no luck.
It's an app where you can record a sound and play it back. Here's the code...
- (IBAction)recordAudio:(id)sender {
if ([self.recordButton.titleLabel.text isEqualToString:#"Record Audio"]){
[self.audioRecorder record];
[self.recordButton setTitle:#"Stop Recording"
forState:UIControlStateNormal];
} else {
[self.audioRecorder stop];
[self.recordButton setTitle:#"Record Audio"
forState:UIControlStateNormal];
// Load the new sound in the audioplayer for playback
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringsByAppendingPaths:#"sound.caf"]];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL error:nil];
}
}
I ran part of your code, and this was the error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-[__NSCFString stringsByAppendingPaths:]: paths argument is not an array'
As the documentation says, stringsByAppendingPaths returns an NSArray of NSString objects made by separately appending each NSString in an NSArray of paths to the receiver. #"sound.caf" is an NSString, not an NSArray, which raises the exception.
Change the following:
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringsByAppendingPaths:#"sound.caf"]];
To:
NSURL *soundFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory()
stringByAppendingPathComponent:#"sound.caf"]];
and it should work.

App crash after '

My application is crashing when a ' appears in the title of the JSON code i'm parsing.
This line is loading the title's:
[[cell textLabel] setText:[item objectForKey:#"title"]];
JSON:
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:#"**test.php"]
encoding:NSStringEncodingConversionAllowLossy
error:nil];
// Create parser
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
parser = nil;
// Set tableData
[self setTableData:[results objectForKey:#"items"]];
Crash:
tableData NSArray * 0x00000001
2012-04-10 10:29:11.446 *[21222:f803] -[NSNull isEqualToString:]:
unrecognized selector sent to instance 0x146ace8 2012-04-10
10:29:11.447 *[21222:f803] * Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[NSNull
isEqualToString:]: unrecognized selector sent to instance 0x146ace8'
* First throw call stack: (0x13d4022 0x1565cd6 0x13d5cbd 0x133aed0 0x133acb2 0x15e0ff 0x2b10 0xb8c54 0xb93ce 0xa4cbd 0xb36f1 0x5cd21
0x13d5e42 0x1d8c679 0x1d96579 0x1d1b4f7 0x1d1d3f6 0x1db81ce 0x1db8003
0x13a8936 0x13a83d7 0x130b790 0x130ad84 0x130ac9b 0x12bd7d8 0x12bd88a
0x1e626 0x1ded 0x1d55) terminate called throwing an exception(lldb)
The key part of that crash info is: unrecognized selector sent to instance ... [NSNull isEqualToString:]
It looks like your JSON contains null somewhere that you are expecting a string. Later on, probably inside of setTableData: you will be doing something like this:
NSString* whatever = [items objectForKey:#"whatever"];
if([whatever isEqualToString:#"hello"]){
...
}
And that will crash, because the whatever variable contains NSNull, not an NSString like you were expecting.

Hpple implementation/Unrecognized selector

I am working with the hpple html parser here: https://github.com/topfunky/hpple
To test the function I've added it to a simple project and am able to compile and open the simulator without errors, but when it is called, I get an unrecognized selector error.
//THIS ACTION SET TO RUN WITH THE PUSH OF A BUTTON
- (IBAction)parseElements{
NSString *urlRequest = item.link;
NSLog(#"urlRequest defined.");
NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"htmlData created.");
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSLog(#"xpathParser made.");
NSString *queriedItem = #"[#class='title']";
// THE APP FAILS WHILE TRYING TO EXECUTE THE NEXT LINE
NSArray *elements = [xpathParser searchWithXPathQuery:queriedItem];
NSLog(#"elements searched.");
TFHppleElement *element = [elements objectAtIndex:0];
NSLog(#"element recalled.");
NSString *storyTitle = [element content];
NSLog(#"The title of the story is: %#", storyTitle);
}
The NSLogs manage to display through "xpathParser made" and then I receive this unrecognized selector message:
-[__NSCFString bytes]: unrecognized selector sent to instance 0x6a52d60
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bytes]: unrecognized selector sent to instance 0x6a52d60'
* First throw call stack:
(0x16c8052 0x1859d0a 0x16c9ced 0x162ef00 0x162ece2 0x495c 0x352e 0x2e3f 0x16c9ec9 0x1395c2 0x13955a 0x1deb76 0x1df03f 0x1de2fe 0x15ea30 0x15ec56 0x145384 0x138aa9 0x15b2fa9 0x169c1c5 0x1601022 0x15ff90a 0x15fedb4 0x15feccb 0x15b1879 0x15b193e 0x136a9b 0x2658 0x25b5)
terminate called throwing an exception
I understand it doesn't like SOMETHING, but what is causing the glitch or are additional frameworks/imports necessary for proper execution? As it stands I have UIKit, viewcontroller.h and TFHpple.h set as the only imports in that file.
Here's your problem:
NSData *htmlData = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlRequest] encoding:NSUTF8StringEncoding error:nil];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
TFHpple's initWithHTMLData is supposed to take an NSData. You declare htmlData to be an NSData, but the actual object you're assigning to it is an NSString.
This should fix it:
NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: urlRequest]];

Converting NSMutableString to NSUrl in Objective C

I actually stuck with a problem while parsing xml-file from a dynamic url in Objective-C.
The Parser works fine for the whole project, but now I have to set a dynamic URL to parse the needed XML-File.
I have two Variables: One for the BaseURL and one for the Params.
Here is my documented Code:
//The Baseurl
NSMutableString* baselink = [NSMutableString stringWithString:zimmertyp.typlink];
//Adds the params to URL
[baselink appendString:aSlice.link];
//In Log it shows the right url to my XML-File with params
NSLog(#"Selected-URL: %#", url);
//I tried to convert the String to NSURL here
NSURL *url = [NSURL URLWithString: baselink];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
self.navigationItem.prompt = #" ";
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
Everytime the Parser tries to walk through, I get:
2011-02-16 16:40:03.371 Project[15566:207] Selected-URL: http://xml.projectwebsite.de/price/doppelzimmer.xml
?Zimmertyp=Doppelzimmer+Classic
2011-02-16 16:40:03.373 Project[15566:207] Error Error Error!!!
2011-02-16 16:40:03.374 Project[15566:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString stringByAppendingString:]: nil argument'
It seems like the NSUrl is empty, or am I wrong?
The App crashes right at the point when the Parser initialized with the url.
Can anyone tell me what's my fault?
greeting,
Zarakas
I solved the problem by myself. The AppendString method added whitespaces to the String... dunno why. I escaped the spaces from the string. Now parser works fine.. ;)