issue with array - iphone

Getting error in this line :
NSString *nmm =[narr objectAtIndex:1];
error shows :
'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index
(1) beyond bounds (1)'

It looks like your array only got one value (which you can accesss at index 0, not index 1).

You should probably start by checking the contents of narr at run time. It sounds like the contents aren't what you would expect them to be at the desired point in execution. Right before the line you posted in your question, use an NSLog call to log the contents of the array like this:
NSLog(#"Contents of array: %#", narr);
Then run the app and check the console after the error arises. Put some time into learning how to use NSLog, breakpoints, and the GDB console - they will end up saving you lots of frustration when debugging.
Your comments on unset's answer raise another point: Why are you storing multiple pieces of data inside the same string? Wouldn't it be easier to separate name, lname and id into separate strings and place each into its own array cell? Then you could access them using [narr objectAtIndex:] without having to worry about parsing the string every time you need one of those pieces of information.

Related

Sphero AR Example Crashes Randomly

I'm testing the Sphero-AR-SDK found on GitHub (https://github.com/orbotix/Sphero-AR-SDK)
When testing, I seem to have random crashes on connect.
The issue I'm trying to debug is an index out of range error.
*"*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x325102a3 0x3a22d97f 0x3245bb75 0x8cb42b 0x32e270f5 0x324e5683 0x324e4ee9 0x324e3cb7 0x32456ebd 0x32456d49 0x3602d2eb 0x3436c301 0x7f10 0x3188)
libc++abi.dylib: terminate called throwing an exception"*
This happens a fair amount when booting the application but not all the time (sometimes it will get through and play the game).
I was wondering if anyone else was having issues with these random crashes or have I not set it up correctly?
I've had a look at the "Other Linker Flags" in XCode too and it has -all_load.
Also, I've safely checked the only place where I could find an array object access in the .mm files (RKUNBridge.mm) has this line:
[RKDeviceSensorsData *data = [sensors_data.dataFrames objectAtIndex:0];
And I safely checked this array exists and is greater than one to double check it's not this line.
Any ideas or suggestions would be much appreciated.
Thanks for reading.

Last item in UITableView unreachable with textLabel.text

I'm populating a UITableView with an array of items I retrieve from the interwebs.
The code in question selects an item from a list populated from an array from the interwebs, then sends a request back to a different php script->sql table etc based on the item selected. The issue i'm having is the entire list is populating correctly, but when using the last item in the list/tableview the array.textLabel.text is apparently.. non existant. Everything sends off fine, but this one last item (doesnt matter how many items i have in the array the very last one just doesnt ... remain or something) and it selects nothing.
Then when sending the 2nd request, it gives me an out of bounds error because the last item doesn't exist and it says im trying to pull something thats not there.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
This is how it makes the array it uses to populate:
NSArray *listItems = [parsedOutput componentsSeparatedByString:#","];
restList = [[NSArray alloc] initWithArray:listItems];
the row count is based on [restList count]; etc.
and to call the text I use:
restName = [tableView cellForRowAtIndexPath:indexPath];
sendName = restName.textLabel.text;
Any idea why the very last element doesn't seem to exist even though its listing and selectable?
The error tells you exactly what's happening. You are attempting to access an index that is outside the bounds of the array. So either you are accessing the wrong array or you are accessing 1 past the actual end of the array. Try to NSLog the array just before the line the error comes up and see if it indeed has the right elements in it as well as output index.row you are trying to access keeping in mind that arrays are zero-indexed.

Please help me out with this exception

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
In my TableViewController I am loading list of arrays..... with selected its display's its detailedViewController...
When I get back and selected the same index or another index i get this exception NSRangeException...
I dont know what to do ?
Can any one help me out.
The array is empty at that point. A good idea is, add some lines of code like this:
NSLog(#"\n\n here, there are this many items: %d \n\n", [yourArrayName count]);
put that line in everywhere possible, add it in at least ten places.
Open your console ("Run" menu in XCode).
You'll soon figure out what is going wrong! hope it helps.

trying to obtain an objects title variable gives unrecognized selector sent to instance

I have an object which holds a title and an indexReference. I save the object to an array and that works correctly.
I then try to load from the array and populate the tableview.
I use this code.
//fill it with contents
SavedFav *temp = [tableViewData objectAtIndex:indexPath.row];
cell.textLabel.text = temp.title;
I then get an error as the following
2010-07-01 15:42:46.386 Daily Quote[1308:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString title]: unrecognized selector sent to instance 0x23f6688'
What is causing this problem?
Thanks in advance.
"temp" is a string obviously, so it's too many answers to give, either you filled tableViewData with strings and trying to obtain title from a string (which is unrecognized) or you have problem with memory there, without seeing more code it's hard to say.
however try
cell.textLabel.text = temp;
and check what's inside, that will give you a good lead.

A NSMutableArray is destroying my life!

IET ANOTHER EDIT (to increase strangeness)
EDITED to show the relevant part of the code
Hi. There's a strange problem with an NSMutableArray which I'm just not understanding...
Explaining:
I have a NSMutableArray, defined as a property (nonatomic, retain), synthesized, and initialized with 29 elements.
realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];
After the initialization, I can insert elements as I wish and everything seems to be working fine.
While I'm running the application, however, if I insert a new element in the array, I can print the array in the function where I inserted the element, and everything seems ok.
However, when I select a row in the table, and I need to read that array, my application crashes. In fact, it cannot even print the array anymore.
Is there any "magical and logical trick" everybody should know when using a NSMutableArray that a beginner like myself can be missing?
Thanks a lot.
I declare my array as
realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];
I insert objects in my array with
[realSectionNames addObject:[category categoryFirstLetter]];
although I know i can also insert it with
[realSectionNames insertObject:[category categoryFirstLetter] atIndex:i];
where the "i" is the first non-occupied position.
After the insertion, I reload the data of my tableView. Printing the array before or after reloading the data shows it has the desired information.
After that, selecting a row at the table makes the application crash. This realSectionNames is used in several UITableViewDelegate functions, but for the case it doesn't matter. What truly matters is that printing the array in the beginning of the didSelectRowAtIndexPath function crashes everything (and of course, doesn't print anything). I'm pretty sure it's in that line, for printing anything he line before works (example):
NSLog(#"Anything");
NSLog(#"%#", realSectionNames);
gives the output:
2010-03-24 15:16:04.146 myApplicationExperience[3527:207] Anything
[Session started at 2010-03-24 15:16:04 +0000.]
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 3527.
Still not understanding what kind of stupidity I've done this time... maybe it's not too late to follow the career of brain surgeon?
following an answer, i printed
NSLog(#"self=%x", self);
NSLog(#"self=%#", self);
NSLog(#"realSectionNames=%x", realSectionNames);
gives the exact same results in every function (from delegate or not).
NSLog(#"realSections = %#", realSectionNames);
prints well in my viewWillAppear, in the didSelectRowAtIndexPath, and crashes in viewForHeaderInSection. No threading, by the way...
So, without knowing what to do, I'm trying "things"... I changed all references of realSectionNames to self.realSectionNames
printing in the viewForHeaderInSection gives the following problem:
2010-03-24 16:01:44.067 myApplication[4104:207] numberOfSectionsInTableView result -> 1
2010-03-24 16:01:44.068 myApplication[4104:207] viewForHeaderAtSection
2010-03-24 16:01:44.068 myApplication[4104:207] self=3d13470
2010-03-24 16:01:44.068 myApplication[4104:207] self=<RootViewController: 0x3d13470>
2010-03-24 16:01:44.069 myApplication[4104:207] self.realSectionNames=3b12830
2010-03-24 16:01:44.070 myApplication[4104:207] realSections = (
<__NSArrayReverseEnumerator: 0x3b50500>
)
2010-03-24 16:01:44.070 myApplication[4104:207] *** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500
2010-03-24 16:01:44.071 myApplication[4104:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500'
2010-03-24 16:01:44.072 myApplication[4104:207] Stack: (
31073371,
2572170505,
31455291,
31024758,
30877378,
276908,
26404,
3227182,
4544033,
4551926,
4550923,
3267462,
3207973,
3249408,
25927,
3222086,
3205252,
459178,
30857920,
30854216,
39163413,
39163610,
2949039
)
What is a NSArrayReverseEnumerator??? And why is it being mean to me???
Note to everybody (and specially to self)...
I'm a dumb, dumb boy.
I was releasing an element which I inserted in the array.
Sorry for the questions, thanks for the answers...
When an application crashes, the Xcode Console will report the error that caused the application to crash. You will want to edit your question to include this error message, along with relevant source code, as this will help others answer your question.
I suspect your selected table row is trying to point to an index in the array, which does not exist. If so, you're trying to reference a part of the array that does not exist. This causes your application to throw an exception and quit.
Remember that an NSMutableArray can be initialized to a certain capacity, but until items are inserted, it does not actually hold any objects. The -initWithCapacity: method only sets aside a certain amount of memory, but there are no placeholders or nil entries for 1 through n indices.
1) Could we know the error message ? and the relevant part of the code ?
2) Here is the proper way to declare your array :
self.realSectionNames = [[NSMutableArray arrayWithCapacity:29];
I suspect your TableView's delegate isn't hooked up correctly, or is connected to an invalid object. Are any of your other delegate methods working? Try putting these at the top of didSelectRowAtIndexPath to check the pointer values:
NSLog(#"self=%x", self);
NSLog(#"self=%#", self);
NSLog(#"realSectionNames=%x", realSectionNames);