josef
josef

Reputation: 1632

An empty Array making the Error

Its seams to be everything working but then I got an Error :

  2014-06-17 21:43:49.819 MotionGraphs[1825:907] FFTW-XXXXXXXXX [0]: 512309.971680 , 0.000000 
2014-06-17 21:43:49.831 MotionGraphs[1825:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x319662a3 0x395e697f 0x318b1b75 0xbad69 0x31f18f41 0x321fbd41 0x321f35c1 0x3226bbe3 0x399fe11f 0x399fd4b7 0x399fedcb 0x31939f3b 0x318acebd 0x318acd49 0x3545f2eb 0x337c2301 0xb68f5 0x39a1db20)
libc++abi.dylib: terminate called throwing an exception

I am almost finishing this App. This is a part of my code ( sorry if it is too long but I am afraid that I am making a beginner mistake):

int SIZE =64;
NSMutableArray  *arrayFftwXRe= [NSMutableArray arrayWithCapacity:SIZE];..
NSTimeInterval delta = 0.005;
NSTimeInterval updateInterval = deviceMotionMin + (delta * sliderValue);
NSMutableArray  *sensorData= [NSMutableArray arrayWithCapacity:SIZE]; ...
if ([mManager isDeviceMotionAvailable] == YES) {
    [mManager setDeviceMotionUpdateInterval:updateInterval];
    [mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
        // saving data
        NSString *strSensorData = [NSString stringWithFormat:@"%.6f %.6f %.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x, deviceMotion.userAcceleration.y, deviceMotion.userAcceleration.z];
        NSString *strSensorDataX = [NSString stringWithFormat:@"%.6f %.6f ", deviceMotion.timestamp, deviceMotion.userAcceleration.x];
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex: 0];
        NSString *docFile = [docDir stringByAppendingPathComponent: @"sensordata.txt"];
        NSString *docFileX = [docDir stringByAppendingPathComponent: @"dataFFTWX.txt"];
               if (![fileManager fileExistsAtPath:docDir]){
            [fileManager createFileAtPath:docFile
                                 contents:nil
                               attributes:nil];
            [fileManager createFileAtPath:docFileX
                                 contents:nil
                               attributes:nil];
                    }
        [ sensorData.description writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
                /////////////////////////////////////// FFTW
                fftw_complex    *data, *fft_result;
        fftw_plan       plan_forward;
        int             i;
        data      = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
        fft_result  = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);

        plan_forward  = fftw_plan_dft_1d(SIZE, data, fft_result,
                                         FFTW_FORWARD, FFTW_ESTIMATE);
        /////////////////////////////////////// Data-X-Y-Z /////////////////
        switch (weakSelf.segmentedControl.selectedSegmentIndex) {
            case kDeviceMotionGraphTypeUserAcceleration:
                [weakSelf setLabelValueX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
                // userAcceleration
                [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeUserAcceleration] addX:deviceMotion.userAcceleration.x y:deviceMotion.userAcceleration.y z:deviceMotion.userAcceleration.z];
                // NSLog(@"Data : %@ ", sensorData  );
                break;
                /////////////////////////////////////// Data-X ////////////////////
            case kDeviceMotionGraphTypeFftwX:
                for( i = 0 ; i < SIZE ; i++ ) {
                    data[i][0] = [sensorDataX [i] floatValue];
                    data[i][1] = 0.0;
                }
                    fftw_execute( plan_forward );
                for( i = 0 ; i < SIZE ; i++ ) {
                    NSLog(@"FFTW-XXXXXXXXX [%d]: %f , %f ",i, fft_result[i][0], fft_result[i][1] ); // working
       fft_result[i][0]=[ arrayFftwXRe[i] floatValue];   // **Here is my ERROR**
                    fft_result[i][1]=[ arrayFftwXIm[i] floatValue];    // **Here is my ERROR TOO**
                    ////////////////show Graph
                    [ arrayFftwXRe.description writeToFile:docFileX atomically:YES encoding:NSUTF8StringEncoding error:nil];
                    [weakSelf setLabelValueX:fft_result[i][0] y:fft_result[i][1] z:deviceMotion.userAcceleration.z];
                    [[weakSelf.graphViews objectAtIndex:kDeviceMotionGraphTypeFftwX] addX:fft_result[i][0] y: fft_result[i][1] z:4];
                }
                break;

Every Help it would be great. Should I change something in this code ( to make it better)?

Upvotes: 0

Views: 110

Answers (1)

Cooper Buckingham
Cooper Buckingham

Reputation: 2534

Whatever arrayFftwXRe is, is empty. Your code at:

fft_result[i][0]=[ arrayFftwXRe[i] floatValue];

is asking for index 0 of arrayFftwXRe and arrayFftwXRe is empty / not initialized. You'll need to look for the section of code that pertains to it.

CODE FROM COMMENT:

The line of code above does:

  1. arrayFftwXRe[i] //this fails because the array is empty or uninitialized
  2. take the value from step 1 (already crashed remember) and get its floatValue
  3. assign the value from step 2 into fft_result[i][0] (but you crashed a long time ago

Upvotes: 1

Related Questions