Bader
Bader

Reputation: 3774

NSInvalidArgumentException in programming iphone

I am still new in objective-c programming, I got this exception, but I don't know what does it mean, The exception is here, and after it there is the code ..

Running…
2011-11-02 12:10:31.923 app3[1322:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Person<0x100001098> init]: cannot init a class object.'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff8499c7b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff84729f03 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff849f5f29 +[NSObject(NSObject) init] + 137
    3   app3                                0x0000000100000d99 main + 124
    4   app3                                0x0000000100000c60 start + 52
    5   ???                                 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

and here is the code that causes this exception , this code is only create class(interface) and makes an instance of this class tand set values ..

#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
@interface Person:NSObject{

    int age;
    int weight;
}

-(void) print;
-(void) setAge:(int) a;
-(void) setWeight:(int) w;

@end


@implementation Person
-(void) print{
    NSLog(@"I am %i years old and weight %i pound",age,weight);
}
-(void) setAge:(int) a{
    age = a;
}
-(void) setWeight:(int)w{
    weight = w;
}
@end

int main(int argc, char *argv[]){
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
    Person *adham;
    adham = [Person alloc];
    adham = [Person init];
    [adham setAge:24];
    [adham setWeight:68];
    [adham print];
    [pool release];
    [pool retain];
    return 0;

}

Upvotes: 0

Views: 1142

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

That should be split like this,

Person *adham;
adham = [Person alloc]; // alloc is a class method
adham = [adham init]; // init is an instance method

Upvotes: 1

jbat100
jbat100

Reputation: 16827

This is wrong:

adham = [Person alloc];
adham = [Person init];

alloc is a class method, init is an instance method, you should be doing this

adham = [[Person alloc] init];

Also, looking at your two lines at the end

[pool release];
[pool retain];

why are you retaining the NSAutoreleasePool? you shouldn't be (you should get a crash in fact). Release the pool and return.

Upvotes: 8

Related Questions