[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0' - iphone

i am creating custom cell.
In that i added 3 textfields. so i want to store that textfield values into nsmutablearray.
when i am trying this code
UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle = UITextBorderStyleRoundedRect;
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];
i am getting error like this
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
so please tell me the reason
thanks in advance.

UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle = UITextBorderStyleRoundedRect;
valueField.text = #"";
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];
the above code will work fine for you.

The text attribute of UITextField is nil by default. Set it to an empty string before adding it to your array, though I think this is not what you are trying to achieve.

Just add this condition to check if the textfield is empty (i.e. textfield.text = nil):
if (valueField.text != nil) {
[array1 addObject:valueField.text];
}
else {
[array1 addObject:#""];
}
This will check if textfield is empty it will add an empty string. If you dont want that just skip the else part.

I noticed you said you declared your array like so:
" declared as like NSMutableArray *array1;"
Have you alloc your array before adding objects to it?
In other words have you done this ?
NSMutableArray *array1 = [[NSMutableArray alloc] init];
If you only did
NSMutableArray *array1;
That will only declare a pointer to a NSMutableArray object. It doesn't point to an instance of a NSMutableArray object.

When you put [array1 addObject:valueField.text];, you are adding a nil object to your array, just as your error says. How can valueField.text be equal to something when you just initialized and allocated valueField?

Related

App crash in button

I have face strange problem on UIButton.
When i tap button the app is crash .
I wrote below code for that...
-(IBAction)renameTest:(id)sender
{
NSLog(#"Tapped");
// UIButton *button = (UIButton *)sender;
NSUInteger row = 1;//button.tag;
NSString * titlename = [titleArray objectAtIndex:row];
RenameTest *renameVC = [[RenameTest alloc]initWithNibName:#"RenameTest" bundle:nil];
renameVC.titlespell = titlename;
NSLog(#"titlespell = %#",renameVC.titlespell);
NSLog(#"title = %#",titlename);
// [button release];
[self.navigationController pushViewController:renameVC animated:YES]; //here APP is cresh
[renameVC release];
}
I check also my .Xib file name .It is ok and files are there.
error msg is below :
2012-07-11 14:28:29.079 TestApp[238:207] -[__NSCFDictionary _isNaturallyRTL]: unrecognized selector sent to instance 0x73d8a80
Thanks in Advance.
[button release] is causing the problem. Remove it and check.
_isNaturallyRTL is an NSString method (private), and it looks like you are passing a dictionary instead of a string somewhere.
Breaking on the exception and showing us the call stack at that point would help tremendously.
If u have created the button in xib file then u cannot release it because you have not allocated it and claimed ownership.. U should call release only on objects you have allocated by calling alloc..
Remove the [button release] statement .. that should fix the crash!
You have a crash which is related to a dictionary and your titlename string is set equal to, titleArray objectAtIndex:row. I believe, without seeing the declaration of your variables, that titleArray is a dictionary, or is a NSMutableArray importing from a plist of dictionaries, either way you need to use objectForKey, when using dictionaries, like this:
[[titleArray objectAtIndex:(NSUInteger *)] objectForKey:(NSString *)]
Obviously replace (NSUInteger *) with your integer row and (NSString *) with the name of your key. This may not be the answer but from your crash report and visible code, this is what I assume.

Loop through UITextField's and store double values

I'm having trouble looping through a set of dynamically created UITextFields and storing those values as a double to be added to an array later on. I'm still pretty new to obj-c programming so bear with me if this question seems trivial. Thanks. This is what I have so far.
NSMutableArray *textFieldCashArray = [[NSMutableArray alloc] init];
double textFieldCash;
for (int y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
P.S and here is the error message I'm getting
Pro[962:b303] -[UIControl text]: unrecognized selector sent to instance 0x680f850
2012-04-01 16:05:46.305 iFinance Pro[962:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControl text]: unrecognized selector sent to instance 0x680f850'
*** Call stack at first throw:
I think what's going on here is that the loop variable is being used as the tag, and the loop starts at zero. viewWithTag will answer either the receiver or one of it's subviews with a given tag, so if the view controller's main view has tag==0 (which it probably does), your first text request is being sent to that top-level view.
Try setting the text field tags to some non-zero value, starting at SOME_OFFSET. Then in your loop:
for (y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y+SOME_OFFSET];
// ...
}
danh is most certainly right about the cause and solution to this problem. Just to add a little, cases like this can be somewhat avoided by checking the Class before casting.
if ([[self.view viewWithTag:y] isKindOfClass:[UITextField class]]) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
You should create array at first:
NSMutableArray *textFieldCashArray = [NSMutableArray array];
Edit
Your error log shows that you are receiving some another UIControl object (UIBtton for example) instead of UITextField. Check your tags on xib (or algorith if you set programmatically) and be sure that UITextField objects has corresponding tags

NSArray of UITextFields, how can i find a specific one?

i loaded up an array with UITextFields, they are all created dynamically (and locally) and i need to keep reference to them for a later point.
my text fields are name, textField01, textField02, etc.
i want to pull out textField02 out and change the data. how can i search through my array of them get it out? i tried "isEqualToString" and failed. i can't use the .tag cause i'm using that for something else. i can't compare to the .text value cause i don't know what it will be (entered by user).
You could use NSDictionary here. Add each text field with a named key that you'll use for retrieval later.
If you are naming them sequentially why not just add them to an NSMutableArray
Then you can get at them by index.
NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:10];
UITextField *textField = nil;
for (int i = 0; i < 10; i++) {
textField = [[UITextField alloc] initWithFrame:myFrame];
[self.view addSubview:textField];
[textFields addObject:textField];
[textField release]; textField = nil;
}
Later on
UITextField *myTextField = [textFields objectAtIndex:1];

[__NSArrayM synchronize]: unrecognized selector exception

I used some NSMutableArray with UIImageView objects.
When I quit the app I get this exception message:
[__NSArrayM synchronize]: unrecognized selector sent to instance 0x4b3a910
In AppDelagate.m
NSMutableArray * aViewArray;
...
currentView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:displayImageNamed]];
[aViewArray addObject:currentView];
[currentView release];
In ViewController.m
UIImageView *myImage = [aViewArray objectAtIndex:i];
myImage.xxx = yyy ;
Looking at your comment regarding NSUserDefaults, you should not be releasing both languages and defaults since you don't have ownership (you didn't alloc/init them). Try removing those two calls to release and see if that resolves your issue.

Accessing a UITextField from an array in Objective-C

I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good. I want to reference those objects when the UISlider value changes. Right now I'm storing those objects in a NSMutableArray and accessing them like so from the sliderChanged method:
NSInteger labelIndex = [newText intValue];
labelIndex--;
NSUInteger firstValue = (int)0;
NSMutableArray *holeArray = [pointsArray objectAtIndex:labelIndex];
UITextField *textField = [textFieldArray objectAtIndex:firstValue];
NSString *newLabel1Text = [[NSString alloc] initWithString:[[holeArray objectAtIndex:firstValue] stringValue]];
[textField setText: newLabel1Text];
[newLabel1Text release];
Everything is working good, but the program crashes on the setText: method. The last message I get from the program is: [UILabel drawTextInRect:] and then I get a EXC_BAD_ACCESS failure.
I want to be able to acces that dynamically created UITextField, but I must be going about it the wrong way.
Thanks!
Uh, yea, you create a text field, but you aren't displaying the field itself, just creating it.
If you want to do what I think you want to do, I would just do if statements.
ex.
if (firstValue == 1)
{
fieldone.text = #"whatever";
}
else if (firstValue == 2)
{
fieldtwo.text = #"whatever";
}