new in objective c (EXC_BAD_ACCESS) problem

Discussion in 'Mac Programming' started by kurushetra, Mar 26, 2009.

  1. kurushetra macrumors newbie

    Mar 19, 2009
    hello i am new in objective c and i have a question ¿why i get a (EXC_BAD_ACCESS) when i call the method "primera"?
    the code is this:
    #import <Cocoa/Cocoa.h>

    @interface cont : NSObject {
    IBOutlet id text;
    - (IBAction)boton:(id)sender;

    #import "cont.h"

    @implementation cont
    NSString *mensaje;
    - (IBAction)boton:(id)sender {
    mensaje = (@"hola");
    cont *con;
    [con primera];
    [text setStringValue:(mensaje)];

    i know i can run the action from (- (IBAction)boton:(id)sender) but this is only one example. why is not possible to call another method from (-(IBAction)boton:(id)sender) also if the method "primera" is with (+) give me a warning (instance variable "text" accesed in class method ) ¿can somebody tell me how i can call the method "primera" from ((IBAction)boton:(id)sender)?
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    What is con supposed to be? In your code it's a pointer to a cont, but it's never initialised so it's just pointing at no-where. It's certainly not pointing at the instance of the class that the method is within if that's what you were hoping.

    Go back to basic C. If I declare a pointer and then never set where it points to what happens when I deference that pointer? The program crashes. Same here. In all seriousness you need to ensure you know your C as this is a basic-level failure.

    I suggest you read Apple's Introduction to Objective-C. You should have read and understood all of this before trying to write any code. It will tell you how to call methods on the same object as the current method is operating on, specifically in this section.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Just say:
    [self primera];
    Right now you are declaring a new cont *, not initializing it, then trying to send it the message primera. self is the instance of cont you actually want to send that message to.


    Edit: I won't erase this since i've already posted it, but robbieduncan's point is good... this is not particularly advanced, and you should understand why things went so wrong, not just use the code above and continue without learning anything.
  4. kurushetra thread starter macrumors newbie

    Mar 19, 2009

Share This Page