Uncaught exception 'NSInvalidArgumentException" help!

Discussion in 'Mac Programming' started by steventhai, Sep 17, 2011.

  1. steventhai, Sep 17, 2011
    Last edited: Sep 17, 2011

    steventhai macrumors newbie

    Joined:
    Sep 17, 2011
    #1
    Hi everyone,

    I am working on a command line tool program in Mac. In main, I have the following codes:

    Code:
    Fraction *p = [[Fraction alloc] initNumerator: 1 andDenominator: 4];
    
    Mixed *a = [[Mixed alloc] initWhole:-1 andFractional: p];
    
    The code for initWhole:andFractional: in the Mixed class is:

    Code:
    - (id) initWhole: (int) w andFraction: (Fraction *) f
    {
        if (self == [super init])
        {
            whole = w;
            fractional = [[Fraction alloc] initNumerator: f.numerator andDenominator: f.denominator];
        }
        
        return self;
    }
    
    And the interface of Mixed class:
    Code:
    @interface Mixed : NSObject 
    {
        //data
        int whole;
        Fraction *fractional;
    }
    
    The exception happened at the Mixed code in main above!!!

    PS: The Fraction class is in good shape.

    Please help me fix this issue. Thanks.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    If that's your actual code, then this:
    Code:
    Mixed *a = [[Mixed alloc] initWhole:-1 [COLOR="Red"]andFractional[/COLOR]: p];
    
    is not spelled the same as this:
    Code:
    - (id) initWhole: (int) w [COLOR="red"]andFraction[/COLOR]: (Fraction *) f
    
    Accuracy is important in programming. So is completeness. Therefore, if the above doesn't pinpoint the problem, copy and paste your actual code, in its entirety.

    Also post the complete actual text of the error message.
     
  3. steventhai thread starter macrumors newbie

    Joined:
    Sep 17, 2011
    #3
    Hi,

    Thanks a lot, it was my false. I mistyped the "andFraction", so I could not understand why the code did not work because there was nothing wrong with it.

    Thanks again.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    If you mistype or misspell certain things in your code, you should get a warning when you compile it. I recommend not ignoring warnings.
     
  5. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #5
    Going to go all "me too", in C-based languages(C, C++, Objective-C etc) warnings are usually a much bigger deal than they are in higher level languages. For instance, many, though certainly not all, Java warnings can usually be safely ignored(even if you shouldn't do so). However compiler warnings in (Objective)C(++) usually mean that there is a good chance your code will fail at run-time.
     
  6. steventhai thread starter macrumors newbie

    Joined:
    Sep 17, 2011
    #6
    I did not get the warning, but I got the exception as typed above.

    Best.
     

Share This Page