Search code examples
objective-cxcodeuncaught-exception

My calculator app crashes with this error message: libc++abi.dylib: terminating with uncaught exception of type NSException


I am absolutely new to Xcode and the world of iOS programming. My first effort to learn it was through a simple calculator app. Everything was working fine until I made a small change in my ViewController.h file by renaming calculatordisplay class to calculatorDisplay (D in capitals). Have reverted back to the old one now too.

Since then whenever I have connected any number buttons to the code and pressed it in the simulator, my app has crashed. I have deleted the old connections, remade them. There are no exclamation marks anywhere in Received connections menu. Tried the Refactor option too to revert back to my old working code. Even I have tried to make a new calculator from scratch and in that too I am getting this same uncaught exception error as libc++abi.dylib: terminating with uncaught exception of type NSException.

Have been fighting with it since morning but in vain!

Would highly appreciate if somebody would help me out on this?

Thank You!

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *calculatordisplay;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.calculatordisplay.text = @"";
}

- (IBAction)calculatordisplay: (UIButton*)buttonPressed {
    NSString *digit;
    digit = [buttonPressed currentTitle];
    NSString *calculatordisplay = [NSString stringWithString: self.calculatordisplay.text];
    self.calculatordisplay.text = [calculatordisplay stringByAppendingString:[buttonPressed currentTitle]];
}

- (IBAction)clear:(UIButton*)buttonPressed {
    self.calculatordisplay.text = @"";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

Output:

2015-09-19 20:33:45.808 MyCalculator[8226:339440] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106e4af65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001068c4deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106e4ae9d +[NSException raise:format:] + 205
    3   Foundation                          0x00000001064b5d9d -[NSPlaceholderString initWithString:] + 96
    4   Foundation                          0x000000010646b2f9 +[NSString stringWithString:] + 45
    5   MyCalculator                        0x00000001063c3873 -[ViewController calculatordisplay:] + 195
    6   UIKit                               0x00000001071f91fa -[UIApplication sendAction:to:from:forEvent:] + 92
    7   UIKit                               0x000000010735d504 -[UIControl sendAction:to:forEvent:] + 67
    8   UIKit                               0x000000010735d7d0 -[UIControl _sendActionsForEvents:withEvent:] + 311
    9   UIKit                               0x000000010735c906 -[UIControl touchesEnded:withEvent:] + 601
    10  UIKit                               0x0000000107263aa3 -[UIWindow _sendTouchesForEvent:] + 835
    11  UIKit                               0x0000000107264691 -[UIWindow sendEvent:] + 865
    12  UIKit                               0x0000000107216752 -[UIApplication sendEvent:] + 263
    13  UIKit                               0x00000001071f1fcc _UIApplicationHandleEventQueue + 6693
    14  CoreFoundation                      0x0000000106d770a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x0000000106d6cfcc __CFRunLoopDoSources0 + 556
    16  CoreFoundation                      0x0000000106d6c483 __CFRunLoopRun + 867
    17  CoreFoundation                      0x0000000106d6be98 CFRunLoopRunSpecific + 488
    18  GraphicsServices                    0x000000010a547ad2 GSEventRunModal + 161
    19  UIKit                               0x00000001071f7676 UIApplicationMain + 171
    20  MyCalculator                        0x00000001063c3dcf main + 111
    21  libdyld.dylib                       0x00000001094d692d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Solution

  • I would check the strings that you're trying to parse

    [NSPlaceholderString initWithString:] + 96
    

    After you initialize the string, it looks like the error/exception occurs right when you're setting your NSString.

    Is the button set with a string value that gets passed on? In other words, when you create the "9" button vs the "3" button how are you differentiating it and then parsing the required operation? I suspect that you need to check the label of the button.