'NSRangeException' while reading data from a file - iphone

I am trying to save data when the home button is pressed.
Here is my relevant code.
- (NSString *) saveFilePath
{
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[path objectAtIndex:0] stringByAppendingPathComponent:#"savefile.plist"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:askedQuestions], //for questionLabel
[NSNumber numberWithInt:timeMin], //timeMin
[NSNumber numberWithInt:timeSec], //timeSec
[NSNumber numberWithInt:skipCount], //skipped question
[NSNumber numberWithInt:correctAnswers], //corectAnswers
nil];
[values writeToFile:[self saveFilePath] atomically:YES];
}
saveFilePath method is providing the file path.
And in viewDidLoad
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
_readerLabel.text = [[values objectAtIndex:0] stringValue];
askedQuestions=[[values objectAtIndex:0] intValue];
timeMin=[[values objectAtIndex:1]intValue];
timeSec=[[values objectAtIndex:2]intValue];
skipCount=[[values objectAtIndex:3]intValue];
correctAnswers=[[values objectAtIndex:4]intValue];
}
UIApplication *myApp = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:myApp];
and i am getting this:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
*** First throw call stack:
(0x16a1012 0x13aee7e 0x16a0deb 0x16957e0 0x6891 0x312d 0x305817 0x305882 0x254a25 0x254dbf 0x254f55 0x25df67 0x221fcc 0x222fab 0x234315 0x23524b 0x226cf8 0x2703df9 0x2703ad0 0x1616bf5 0x1616962 0x1647bb6 0x1646f44 0x1646e1b 0x2227da 0x22465c 0x26cd 0x25f5)
libc++abi.dylib: terminate called throwing an exception
Where is the problem in the code. Please help!

You're not checking to see if anything has worked!
What happens if
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
either fails (the file might not exist) or if it returns an array with 0 items in?
You need to add something like :
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
if (values.count > 0) {
...
}
You should also set a breakpoint on applicationDidEnterBackground: and step though to make sure that your save method is working as you expect.
Also, I suspect one of these two lines is wrong :
_readerLabel.text = [[values objectAtIndex:0] stringValue];
askedQuestions=[[values objectAtIndex:0] intValue];
they're both asking for the item at index 0

Related

GDataXml Parsing Issues in iPhone

here this is below the code i need to parse the section and Company Sub data Using GDataXml in iOS.
-<MainList>
<FirstMessage>Right</FirstMessage>
<SecondMessage>Wrong</SecondMessage>
<Status>0</Status>
-<CompanyList>
-<Company>
<Address1>12447 Hedges Run Drive</Address1>
<Address2>#B-1 </Address2>
<City>Lake Ridge</City>
<CompanyName>Starbucks</CompanyName>
<CreatedBy>example#example.com</CreatedBy>
-</Company>
-<Company>
<Address1>12447 Hedges Run Drive</Address1>
<Address2>#B-1 </Address2>
<City>Lake Ridge</City>
<CompanyName>Starbucks</CompanyName>
<CreatedBy>example#example.com</CreatedBy>
-</Company>
</CompanyList>
</MainList>
Here is My Try i am getting every thing fine but i am getting nothing in NSArray arr.
NSArray *channels = [rootElement elementsForName:#"CompanyList"];
for (GDataXMLElement *icon in channels) {
NSArray *arr = [icon elementsForName:#"Company"];
if ([arr count]>0)
{
NSlog(#"Done");
}
}
See this answer, this answer is helpful for u. NSXMLParser example
Hello guys i figured it out as follows
GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error];
NSLog(#"Error:%#",[error description]);
NSArray *getData = [[xmlDocument rootElement]elementsForName:#"CompanyList"];
NSArray *tempArray = [[NSMutableArray alloc]init];
for(GDataXMLElement *e in getData)
{
NSArray *el = [e elementsForName:#"Company"];
for (GDataXMLElement *e in el) {
static int count =0;
NSString *testString = [[[e elementsForName:#"Address1"] objectAtIndex:0] stringValue];
NSLog(#"Company %d : %#",count+1, testString);
}

NSArray componentsSeperatedByString Sigabrt

I get a Sigabrt at the NSlog and i have no idea why - any suggestions?
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [contentList componentsSeparatedByString:#"$$"];
NSLog(#"%#%#",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
Following Joes suggestions, I now got:
NSString* contentList = [NSString stringWithContentsOfFile:currentFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray* contentArray = [[contentList componentsSeparatedByString:#"$$"] retain];
if ([contentArray count] > 0) {
NSLog(#"%#%#",contentList,[contentArray count]);
kunden = [contentArray objectAtIndex:0];
kundenView.text = kunden;
}
Which gives me an EXC_BAD_ACCESS at the NSLog thing.
I get a Sigabrt at the NSlog
Your NSLog statement is trying to print an integer as if it was an object:
NSLog(#"%#%#",contentList,[contentArray count]);
^
Here!
Replace %# with %d.
You can read more on format specifiers in the String Programming Guide.
You are not checking to make sure you have at least 1 element in your array. Accessing [contentArray objectAtIndex:0] will be an issue if the contentArray is empty.

problem in inserting the vlaues into database

i was getting familiar with sqlite and try to insert the values into database but giving exception. tf1 and tf2 are object of textfield. here is my code:
#synthesize tf1, tf2, add, disp;
-(id)initApp
{
if(![super init])
return nil;
//DB stored in application bundle
NSString *DBName = #"kbase.sqlite";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingString:DBName];
db = [FMDatabase databaseWithPath:path];
[db setLogsErrors:TRUE];
[db setTraceExecution:TRUE];
if(![db open])
{
NSLog(#"Could not open Database");
return 0;
}
else
{
NSLog(#"Database opened successfully");
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initApp];
[add addTarget:self action:#selector(insertButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[disp addTarget:self action:#selector(displaytButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(IBAction)insertButtonClicked :(id)sender
{
NSLog(#"insert");
NSLog(#"db.....%#",db);
[db executeUpdate:#"insert into info values (?,?)", tf1.text, tf2.text];
[self resignFirstResponder];
}
-(IBAction)displayButtonClicked:(id)sender
{
NSString *str1 = tf2.text;
NSString *returnResult = [[NSString alloc]init];
FMResultSet *rs = [db executeQuery:#"select name from info where rollno = 1"];
while ([rs next])
{
returnResult = [rs stringForColumn:#"rollno"];
[tf1 setText:returnResult];
[tf2 setText:returnResult];
}
}
databse is opening successfully and controll terminate after printing insert
exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL executeQuery:]: unrecognized selector sent to instance 0x3d30ef0'
i am not able to configure the problem. plz help.
I think make your method
[db executeUpdate:#"insert into info values (?,?)" withValue1:tf1.text withValue2:tf2.text];
Instead of
[db executeUpdate:#"insert into info values (?,?)", tf1.text, tf2.text];
it seems that db does not exist in this context. If db is a class variable, make sure it is not released somewhere else. Simply put an NSLog(#"%#", db); above the execution to test it.

NSArray runtime array

I have got I have got two methods both in different classes. One is class method and other is instance method. i am calling class method from instance method. When instance method finishes it gives runtime error "EXC_BAD_ACCESS".
#import "xmlObject.h"
#import "textmeAppDelegate.h"
#implementation Class1
- (void)method1 {
textmeAppDelegate *del = (textmeAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *bgColor = [[NSArray alloc] initWithArray:[xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:#"backgroundcolor"]]];
UIColor *color = [UIColor colorWithRed:[[bgColor objectAtIndex:3] floatValue] green:[[bgColor objectAtIndex:2] floatValue] blue:[[bgColor objectAtIndex:1] floatValue] alpha:[[bgColor objectAtIndex:0] floatValue]];
CGContextSetFillColor(context, CGColorGetComponents([color CGColor]));
CGContextFillRect(context, rect);
[bgColor release];
}
#end
#implementation xmlObject
+ (NSArray *) fetchImmediateChildrenValues:(NSMutableDictionary *) node {
NSMutableDictionary *tmp = [[node objectForKey:#"children"] retain];
NSArray *keys = [[NSArray alloc] initWithArray:[tmp allKeys]];
keys = [keys sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSMutableArray *pushArr = [[[NSMutableArray alloc] init] autorelease];
NSString *val = [[NSString alloc] init];
for(NSString *str in keys) {
val = (NSString *)[[tmp objectForKey:str] objectForKey:#"innertext"];
[pushArr addObject:val];
}
[val release];
[keys release];
return [NSArray arrayWithArray:pushArr];
}
#end
What is wrong with the code? Also app is crashing for this line of code
application is crashing if i include this line
NSArray *bgColor = [[NSArray alloc] initWithArray:[xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:#"backgroundcolor"]]];
If I remove it application runs smoothly.
I have several comments on your code. One of them is the immediate cause of your crash, but you need to fix at least one other issue too. The short answer is that you over release val and keys.
NSArray *bgColor = [[NSArray alloc] initWithArray:[xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:#"backgroundcolor"]]];
You don't need to create a new array here, you can simply write the following:
NSArray *bgColor = [xmlObject fetchImmediateChildrenValues:[del.navigationbarStyle objectForKey:#"backgroundcolor"]];
if you do, you don't need the [bgColor release] further down.
NSArray *keys = [[NSArray alloc] initWithArray:[tmp allKeys]];
keys = [keys sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
These two lines leak the first NSArray, you alloc it but you overwrite it straight away with the sorted version. In fact, you can simply write:
keys = [[tmp allKeys] sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
Note that you do not own keys so you can get rid of the [keys release] line further down.
NSString *val = [[NSString alloc] init];
for(NSString *str in keys) {
val = (NSString *)[[tmp objectForKey:str] objectForKey:#"innertext"];
[pushArr addObject:val];
}
[val release];
This is the source of your immediate problem. You first alloc a new string. Then you immediately overwrite it on each iteration of your loop. So the allocated NSString leaks. You do not own the val returned by [[tmp objectForKey:str] objectForKey:#"innertext"]; on each iteration, so the release ov val after the loop should not be there.
On a side note, objectForKey: returns an id - the cast to NSString* is redundant. Most people leave it out.
[keys release];
Going back to the bit above where I told you that you were leaking your alloc'd keys? Well the new version of keys you overwrote it with you don't own. Therefore you must not release keys here.
return [NSArray arrayWithArray:pushArr];
This is fine. My preference would be for:
return [[pushArray copy] autorelease];
but it is just a matter of style. You could also just return pushArray, but pushArray is mutable and the caller may rely on the return value being immutable.
Test your code with NSZombieEnabled set... It should give you enough informations to fix your problem.

Problem reading a string from an NSDictionary inside an NSMutableArray stored using NSKeyedArchiver

I'm saving some data using a series of NSDictionaries, stored in an NSMutableArray and archived using NSKeyedArchiver.
I'm basically trying to save the states of several instances the class 'Brick', so I've implemented a getBlueprint method like this (slimmed down version)
-(id)getBlueprint
{
// NOTE: brickColor is a string
NSDictionary *blueprint = [NSDictionary dictionaryWithObjectsAndKeys:
brickColor, #"color",
[NSNumber numberWithInt:rotation], #"rotation",
nil];
return blueprint;
}
And so I have another method that creates a new Brick instance when provided with a blueprint.
-(id)initWithBlueprint:(NSDictionary *)blueprint spriteSheet:(NSString *)ssheet
{
if((self == [super init])){
brickColor = [blueprint objectForKey:#"color"];
[self setColorOffset:brickColor];
while(rotation != [[blueprint objectForKey:#"rotation"] intValue]){
[self setRotation:90];
}
}
return self;
}
Which works when I pass it a 'fresh' blueprint, but not when I read a blueprint from a saved file... sort of. For example, the rotation will work, but changing the color wont. So while I can read the value of brickColor using
NSLog(#"brick color %#", [blueprint objectForKey:#"color"]);
if I try something like
if(brickColor == #"purple"){
colorOffset = CGPointMake(72,36);
NSLog(#"Changed offset for -- %# -- to %#", color, NSStringFromCGPoint(colorOffset));
}
And I know that color is purple, the condition doesn't return true. I thought it might be that somehow NSKeyedUnarchiver changed a string into something else, but the following test returns true.
if([color isKindOfClass:[NSString class]]){
NSLog(#"%# IS A STRING", color);
}else{
NSLog(#"!!!!! COLOR IS A NOT STRING !!!!!");
}
As I said, this isn't a problem if I try to use a freshly created NSDictionary as a blueprint, only when a blueprint is archived and then read back in.
So, as usual, I'm wondering if anyone has any ideas why this might be happening.
incase it's relevant, here's how the data is being stored and recieved.
// Saving
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(void)buildLevelData{
levelData = [[NSMutableArray alloc] initWithCapacity:100];
for(brickSprite *brick in spriteHolder.children){
[levelData addObject:[brick getBlueprint]];
}
}
-(void)saveLevel
{
[self buildLevelData];
NSData *rawDat = [NSKeyedArchiver archivedDataWithRootObject:levelData];
if([self writeApplicationData:rawDat toFile:saveFileName]){
NSLog(#"Data Saved");
}else{
NSLog(#"ERROR SAVING LEVEL DATA!");
}
[[Director sharedDirector] replaceScene:[MainMenu scene]];
}
- (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(#"Documents directory not found!");
return NO;
}
NSString *appFile = [saveDir stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
// Loading
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- (void) loadRandomMapFrom:(NSString *)dir
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
if(!docsDir){
NSLog(#"Cound Not Find Documents Directory When trying To Load Random Map");
return;
}
dir = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#", dir]];
// we'll also set the file name here.
NSArray *existingFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:nil];
// get last file for this test
NSString *filePath = [dir stringByAppendingPathComponent:[existingFiles objectAtIndex:([existingFiles count] - 1)]];
NSMutableArray *levelData = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[self buildMapWithData:levelData];
}
-(void)buildMapWithData:(NSMutableArray *)lData
{
for(NSDictionary *blueprint in lData){
brickSprite *brick = [[brickSprite alloc] initWithBlueprint:blueprint spriteSheet:#"blocks.png"];
[spriteHolder addChild:brick];
}
}
Sorry about the mess of a question. There's a lot going on that I'm struggling to fully understand myself so it's hard to break it down to the bare minimum.
You should always compare strings with [firstString isEqualToString:secondString], because firstString == secondString only checks for pointer equality, e.g. if both strings are stored at the same location (which they'll never be when comparing dynamically created objects and string constants).