Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashing due to NSInvalidArgumentException but can't find it

My app is crashing upon running it with the following debug:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString initWithString:attributes:: nil value'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102a83495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001027e299e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000102a832ad +[NSException raise:format:] + 205
    3   Foundation                          0x00000001023d993e -[NSConcreteMutableAttributedString initWithString:attributes:] + 129
    4   myApp                             0x0000000100006da5 -[WelcomeViewController viewDidLoad] + 373
    5   UIKit                               0x000000010148559e -[UIViewController loadViewIfRequired] + 562
    6   UIKit                               0x0000000101485777 -[UIViewController view] + 29
    7   UIKit                               0x00000001017902e2 -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 390
    8   UIKit                               0x00000001013cbffa -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1109
    9   UIKit                               0x00000001013cbb9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    10  UIKit                               0x00000001013cbaef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
    11  UIKit                               0x00000001013cadfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
    12  UIKit                               0x000000010148970a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
    13  UIKit                               0x00000001013c5b1b -[UIWindow addRootViewControllerViewIfPossible] + 490
    14  UIKit                               0x00000001013c5c70 -[UIWindow _setHidden:forced:] + 282
    15  UIKit                               0x00000001013ceffa -[UIWindow makeKeyAndVisible] + 51
    16  UIKit                               0x000000010138ac98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
    17  UIKit                               0x000000010138ea0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    18  UIKit                               0x000000010139fd4c -[UIApplication handleEvent:withNewEvent:] + 3189
    19  UIKit                               0x00000001013a0216 -[UIApplication sendEvent:] + 79
    20  UIKit                               0x0000000101390086 _UIApplicationHandleEvent + 578
    21  GraphicsServices                    0x00000001047dd71a _PurpleEventCallback + 762
    22  GraphicsServices                    0x00000001047dd1e1 PurpleEventCallback + 35
    23  CoreFoundation                      0x0000000102a05679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    24  CoreFoundation                      0x0000000102a0544e __CFRunLoopDoSource1 + 478
    25  CoreFoundation                      0x0000000102a2e903 __CFRunLoopRun + 1939
    26  CoreFoundation                      0x0000000102a2dd83 CFRunLoopRunSpecific + 467
    27  UIKit                               0x000000010138e2e1 -[UIApplication _run] + 609
    28  UIKit                               0x000000010138fe33 UIApplicationMain + 1010
    29  myApp                             0x0000000100006b03 main + 115
    30  libdyld.dylib                       0x00000001033c25fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

The problem is that I know for a fact I never used NSConcreteMutableAttributedString I have checked and double checked my code to see if maybe I am missing something but I keep coming up blank, any ideas as to what can be done to fix this problem?

Update

I added an exception breakpoint which told me that the following line was causing my app to crash:

 NSMutableAttributedString *thirdGreetingAttr = [[NSMutableAttributedString alloc] initWithString:thirdGreeting attributes:secondGreetingAttrs19];
    self.welcomeUserLabel.attributedText = thirdGreetingAttr;

"Ok, odd" I think to myself, this was working just fine 2 weeks ago when I last touched this project, This is the complete code block:

 NSDictionary *secondGreetingAttrs19 = [NSDictionary dictionaryWithObjectsAndKeys:fitnestFontBold, NSFontAttributeName, nil];
    NSString *thirdGreeting = currentUser.username;
    NSMutableAttributedString *thirdGreetingAttr = [[NSMutableAttributedString alloc] initWithString:thirdGreeting attributes:secondGreetingAttrs19];
    self.welcomeUserLabel.attributedText = thirdGreetingAttr;

So I go ahead and comment out the 2 lines that are causing the issue, and the app runs just fine. I un-comment the lines back to how it was originally and to my surprise build runs JUST FINE :/ what??? Anyone know what could have happened here?

UPDATE

As the chosen answer says, I realized that this error was happening because my username property was returning a null value.

like image 339
vzm Avatar asked Aug 15 '14 01:08

vzm


1 Answers

make sure your thirdGreeting(NSString *thirdGreeting = currentUser.username; ) is not nil value. I think the thireGreeting is nil, you should change to

NSString *thirdGreeting = (currentUser.username.length>0) ? currentUser.username.length : @""; 
like image 174
Suen Avatar answered Oct 12 '22 12:10

Suen