views:

36

answers:

3

Ok for some reason my code wont play nice and I'm too new to cocoa to figure this out on my own..

when the send button is pressed it is meant to run the createEmail method. But it says
GDB: Program received signal: "EXC_BAD_ACCESS". when the button is pressed.

#import "Controller.h"

@implementation Controller
-(IBAction)send:(id)sender{
 [self createEmail];
}

-(void)createEmail{
 NSString *number  = [numfield stringValue]; 
 NSString *carrier = [carrierfield stringValue];
 NSString *carrierTag;

 [carrier lowercaseString]; //make all lowercase 

 //set carrierTag based on what carrier it is
 if ([carrier isEqualToString:@"verizon"]) {
  carrierTag = @"@vtext.com";
 }
 if ([carrier isEqualToString:@"at&t"]) {
  carrierTag = @"@txt.att.net";
 }
 if ([carrier isEqualToString:@"nextel"]) {
  carrierTag = @"@messaging.nextel.com";
 }
 if ([carrier isEqualToString:@"sprint"]) {
  carrierTag = @"@messaging.sprintpcs.com";
 }
 if ([carrier isEqualToString:@"cingular"]) {
  carrierTag = @"@cingularme.com";
 }
 if ([carrier isEqualToString:@"cingular"]) {
  carrierTag = @"@cingularme.com";
 }
 if ([carrier isEqualToString:@"virgin"]) {
  carrierTag = @"@vmobl.com";
 }
 if ([carrier isEqualToString:@"t-mobile"]) {
  carrierTag = @"@tmomail.net";
 }

 //Concatenate number and carrierTag to create an email address
 email = [number stringByAppendingString:carrierTag];
}
@end
A: 

If carrier is none of the handled strings, carrierTag is left uninitialized - you should at least set it to nil and test for that before working with it:

NSString *carrierTag = nil;
// ...
if (carrierTag) { 
    // ...

There is also the problem that you expect lowercaseString to mutate the string - but it just returns a new string that is the lowercase version of the string you called it on. Use e.g. the following instead:

carrier = [carrier lowercaseString];

Note that you could greatly simplify the lookup of the carrier-tags by using a dictionary, e.g.:

NSDictionary *carrierTags = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"@vtext.com", @"verizon",
                               // ...
                               nil];
NSString *carrierTag = [carrierTags objectForKey:carrier ? carrier : @""];
Georg Fritzsche
It's worth mentioning that although initializing to nil is certainly a good first step, it's also not a good idea to pass nil to most AppKit methods, so calling stringByAppendingString: with a nil argument should be avoided as well.
smorgan
@smo: Sure, i didn't mean to imply that.
Georg Fritzsche
+1  A: 

Well, I see two possibilities. Either carrier is not set correctly or it's not equal to any of those strings in which case carrierTag is left uninitialised.

In the former case, the exception is probably on the line:

[carrier lowercaseString]

In the latter, it will probably be at:

email = [number stringByAppendingString:carrierTag];

The debugger should provide you with that information, and you should provide it to us as well :-)

In addition, lowercaseString returns another string, it doesn't operate in-place, so you need:

carrier = [carrier lowercaseString];

What you may want to do is set carrierTag to an initially empty string so that nothing is appended if there is no match:

carrierTag = @"";
if ([carrier isEqualToString:@"verizon"]) {
    carrierTag = @"@vtext.com";
}
:
:
paxdiablo
hmm this is intresting...if i type "Verizon" (note the capital V) it returns error. If i type "verizon" nothing happens...But my program isn't programed to do anything so that means im good? that the capital V is the issue?
OK, I added [bodyfield setStringValue:email]; to see if if would print out the corect thing and it printed,[email protected] So it works. I'll mark it as solved so no one else posts here. thanks
@sho: Standard string comparison is case-sensitive and `lowercaseString` returns a new string ... @pax: Sending messages to `nil` doesn't lead to a bad access.
Georg Fritzsche
A: 

In addition to the problems pointed out in the other answers, email appears to be a member variable, and you are assigning it an autoreleased value--meaning it will become garbage as soon as the event loop finishes, and you'll almost certainly crash the next time you access it. You need to retain the value when you assign it.

If you are new to Cocoa, you should absolutely read Apple's introduction to memory management.

smorgan