Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to fetch data from my NSMutableArray, I am doing this

-(void)ViewDidLoad{
     A_array = [[NSMutableArray alloc] init];
     A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]
    var_Count_Answer_A   = 0;    // int  
}

-(void)method_Second {
    NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);    // This line is crashing if I  click button again. First time line works fine but if we click button again and    var_Count_Answer = 2 then it will crash.

    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]];   // If I comment on NSLOG then this line will crash
    A = [str1 integerValue];
    NSLog(@"A is %d",A);


    var_Count_Answer_A ++;
}

if I try this NSLog(@"%@",[A_array objectAtIndex:5]); // works fine

NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);  // var_Count_Answer_A =5; and crash

Any Idea or Suggestions would be highly appreciated.

share|improve this question
2  
post your crash log. –  Sunny Jul 17 at 6:56
 
where is define var_Count_Answer_A –  kirti mali Jul 17 at 6:59
 
Are you using ARC? Where are all these iVars defined? –  Stavash Jul 17 at 7:01
1  
(not really related to your problem) In your -viewDidLoad, you are calling "A_array = [[NSMutableArray alloc] init];" followed by a "A_array = [NSMutableArray arrrayWithObjects:...]" call. That's not necessary since "[NSMutableArray arrrayWithObjects:...]" allocates and initializes a NSMutableArray for you, filled with the object given to this method –  Markus Jul 17 at 7:04
 
as @Sunny suggested . Put some crash logs otherwise no one will know whats going on ! –  V-Xtreme Jul 17 at 7:05
show 5 more comments

closed as unclear what you're asking by Josh Caswell, Michel Keijzers, Piotr Chojnacki, Dirk, Mohammad Adil Jul 17 at 9:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking.If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

up vote 1 down vote accepted

Use

A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];

Instead you used.

A_array = [[NSMutableArray alloc] init];
     A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];

As I review Your Project is ARC disable try this Working fine for me.

ViewControler.h
@interface MasterViewController : UIViewController 
{
    NSMutableArray *A_array;
    int var_Count_Answer_A;
}

ViewControler.m
-(void)viewDidLoad  {
     [super viewDidLoad];
     A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil];
var_Count_Answer_A   = 0;
     var_Count_Answer_A   = 0;    // int
        }

-(IBAction)method_Second:(id)sender {
     NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);    

     NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]];   // If I comment on NSLOG then this line will crash
     int A = [str1 integerValue];
     NSLog(@"A is %d",A);

     var_Count_Answer_A++;
        }

But As Everyone know at last there must be occur this exception.

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 16 beyond bounds [0 .. 15]'

As trying to access array index object that does not exist. And if you still have some error then First Enable NSZombie in your project Follow this.

-> "Product" menu, select "Edit Scheme", go to the "YourAp.app" stage in the left panel, and the "Arguments" tab on the right. You can then add NSZombieEnabled to the "Environment Variables" section and set the value to YES.

And Now Share your Crash Log.

share|improve this answer
 
this error is showing - symbol stub for: getpid –  Nisha Singh Jul 17 at 7:30
 
Please enable NSZombie as i also updated my answer and share the crash log. –  bit-whacker Jul 17 at 7:34
 
-[__NSArrayM objectAtIndex:]: message sent to deallocated instance 0x71c07a0 –  Nisha Singh Jul 17 at 7:40
 
@NishaSingh as your project is ARC disable now try my updated code only change one line. A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4"‌​,@"2",@"0",@"3",nil]; ` –  bit-whacker Jul 17 at 7:58
 
DownVoter please comment..! –  bit-whacker Jul 17 at 8:43
show 4 more comments

Your code hurt my eyes

There are certain coding standards to be followed while writing code in Objective-C. Every language got one. They make code readable and understandable. You can refer to Cocoa Coding Guidelines and help yourself writing better looking code.

Explain Properly

Without the error message before crash no one can help you. Next time when you post about a crash, also explain the error message you got.

Was it a EXEC_BAD_ACCESS? If so it is the awesome garbage collector sweeping your array object away. Anyway your code sores my eyes and I am taking the privilege refactoring the code. Maybe it will help you.

A little bit of refactoring

NSArray *aArray;
- (void)viewDidLoad
   {
     [super viewDidLoad];
     aArray = @[@"1", @"2", @"3", @"4"];
     // or use[[NSArray alloc]initWithObjects:@"1", @"2", @"3", @"4", @"5"];
     //Why do you need a mutable array while initializing it static?
   }

 - (void)methodSecond
   {
     static int counter = 0;
       if (aArray) {
         if (counter<aArray.count) {
           NSLog(@"A is %u",[[aArray objectAtIndex:counter++]integerValue]);
         } else {
           counter = 0;
           [self methodSecond];
         }
       }
   }

Understand what you are doing

So a detail explanation of what I guess your problem is:

  • You're using [NSArray initWithObjects:] this creates an autoreleased object, and we got no control over its life time. And they get released if they are not referenced immediately after creating, which is probably whats happening your case. In my experience, mutable autoreleased object always sources bad access problems. So it is better to have the object alloc and inited, ie.. [[NSArray alloc]initWithObjects:] and I observe you are not adding/removing array members in run-time. There is no purpose of having a MutableArray.

  • In your code, you're creating two objects. The first array object is allocated and immediately dereferenced in the next line. Which is probably a mistake.

If you want to know more about auto-released objects. Read this.

share|improve this answer
 
Thanks alooot... Your code is fantastic. Its working superb, exactly what i wanted... Thanks Alooottt.... –  Nisha Singh Jul 17 at 9:35
add comment

in your code you are usimng two different varaibles once check thosetwo.

var_Count_Answer and var_Count_Answer_A

share|improve this answer
 
Sorry Sir by mistake I forgot to write _A here in my code both are var_Count_Answer_A only. –  Nisha Singh Jul 17 at 7:05
 
this error is showing - symbol stub for: getpid –  Nisha Singh Jul 17 at 7:30
 
@ Nisha Singh:your code seems to be good nut some where else you went wrong i think so. –  Sunny Jul 17 at 7:38
 
@Sunny i think prob only some how var_Count_Answer_A release and she access this variable at second Method might be in her project ARC enable. i think so only reason is this. –  Nitin Gohel Jul 17 at 7:40
add comment

This error occurs because you are using an auto-released object. When you call like:

A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]

The A_array is an autoreleased object, you can't say when it'll be released. That is why this error occurs.

Solution:

Declare a property for the array like:

@property (nonatomic, strong) NSMutableArray *A_array;

Then modify your methods like:

-(void)ViewDidLoad
{
     _A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]
    var_Count_Answer_A   = 0;    // int  
}

-(void)method_Second
{
    NSLog(@"%@",[_A_array objectAtIndex:var_Count_Answer_A])
    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[_A_array objectAtIndex:var_Count_Answer_A]];
    A = [str1 integerValue];
    NSLog(@"A is %d",A);
    var_Count_Answer_A ++;
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.