Search code examples
iosuitableviewuncaught-exception

UITableView Exception iOS


I am not able to figure out what is causing the application crash. A UITableView is being filled with data using the method numberOfRowsInSection. The first two record loads correctly but when the 3rd row is about to load, the app crashes. Please help.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    myDictionary = self.myArray;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [myDictionary count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"TransHistoryCell";

    TransHistoryCell *cell = (TransHistoryCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
    @try
    {

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TransHistoryCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        NSDictionary *currentDictionary = [myDictionary objectAtIndex:indexPath.row];

        for(NSDictionary *object in currentDictionary)
        {
            NSString *key = [object valueForKey:@"Key"];
            NSString *value = [object valueForKey:@"Value"];
            if([key isEqualToString:DATE])
            {
                cell.dateLabel.text  = value;
            }
            else if([key isEqualToString:TRANSACTION_ID])
            {
                cell.transactionIdLabel.text = value;
            }
            else if([key isEqualToString:THIRD_PARTY])
            {
                cell.thirdPartyLabel.text = value;
            }
            else if([key isEqualToString:AMOUNT])
            {
                cell.amountLabel.text = value;
            }
            else if([key isEqualToString:TRANSACTION_DESC])
            {
                cell.transactionDescLabel.text = value;
            }
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception: %@", exception);
    }
    @finally
    {
        return cell;
    }
}

Exception:

2015-01-14 09:49:12.808 MobileProject[19580:5974561] -[NSNull length]: unrecognized selector sent to instance 0x103796ce0
2015-01-14 09:49:12.812 MobileProject
[19580:5974561] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x103796ce0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001034edf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000102187bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001034f504d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010344d27c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010344ce18 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x00000001009b2f45 -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 65
    6   UIKit                               0x00000001009b2da0 -[UILabel textRectForBounds:limitedToNumberOfLines:] + 76
    7   UIKit                               0x00000001009b6852 -[UILabel _intrinsicSizeWithinSize:] + 170
    8   UIKit                               0x00000001009b6932 -[UILabel intrinsicContentSize] + 76
    9   UIKit                               0x0000000100e6cea4 -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 33
    10  UIKit                               0x0000000100e6cc64 -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 422
    11  UIKit                               0x0000000100e740d6 -[UIView(AdditionalLayoutSupport) updateConstraints] + 163
    12  UIKit                               0x00000001009b679d -[UILabel updateConstraints] + 272
    13  UIKit                               0x0000000100e736fa -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 248
    14  UIKit                               0x0000000100e738f2 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
    15  CoreFoundation                      0x00000001033f6194 CFArrayApplyFunction + 68
    16  UIKit                               0x0000000100e7369b -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 153
    17  Foundation                          0x0000000101aead6e -[NSISEngine withBehaviors:performModifications:] + 155
    18  UIKit                               0x0000000100e738f2 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
    19  CoreFoundation                      0x00000001033f6194 CFArrayApplyFunction + 68
    20  UIKit                               0x0000000100e7369b -[UIView(AdditionalLayoutSupport) _internalUpdateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 153
    21  UIKit                               0x0000000100e738f2 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededAccumulatingViewsNeedingSecondPassAndViewsNeedingBaselineUpdate:] + 124
    22  UIKit                               0x0000000100e73dbe __60-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]_block_invoke + 96
    23  UIKit                               0x0000000100e73a86 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 231
    24  UIKit                               0x0000000100c698b8 -[UITableViewCellContentView updateConstraintsIfNeeded] + 95
    25  UIKit                               0x0000000100e7419e -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 159
    26  UIKit                               0x000000010085ab2d -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 114
    27  UIKit                               0x0000000100866973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    28  QuartzCore                          0x0000000103d6ade8 -[CALayer layoutSublayers] + 150
    29  QuartzCore                          0x0000000103d5fa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    30  QuartzCore                          0x0000000103d5f87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    31  QuartzCore                          0x0000000103ccd63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    32  QuartzCore                          0x0000000103cce74a _ZN2CA11Transaction6commitEv + 390
    33  QuartzCore                          0x0000000103ccedb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    34  CoreFoundation                      0x0000000103422dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    35  CoreFoundation                      0x0000000103422d20 __CFRunLoopDoObservers + 368
    36  CoreFoundation                      0x0000000103418b53 __CFRunLoopRun + 1123
    37  CoreFoundation                      0x0000000103418486 CFRunLoopRunSpecific + 470
    38  GraphicsServices                    0x000000010338b9f0 GSEventRunModal + 161
    39  UIKit                               0x00000001007ed420 UIApplicationMain + 1282
    40  MobileProject                 0x0000000100535c83 main + 115
    41  libdyld.dylib                       0x0000000107323145 start + 1
    42  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • you are trying to get the length of what you think is an NSString but is actually NSNull. so check the NSNULL value in both key's ` Key and value