NSMutableArray is null?

  • Hi everyone:

    I am having a strange problem with NSMutableArray which is that it doesn't
    seem to work with addObjectsFromArray.  I try setting this for a "global
    variable" (forget what they are called in Cocoa) and it just returned null.
    Here is my code (assuming that global Variable is the global variable which
    is a NSMutable Array):

    NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing", nil];
    [globalVariable addObjectsFromArray:testing];

    Thanks for any help.
  • where did you allocate and initialize your globalVariable?

    Pierce Freeman wrote:
    > Hi everyone:
    >
    > I am having a strange problem with NSMutableArray which is that it doesn't
    > seem to work with addObjectsFromArray.  I try setting this for a "global
    > variable" (forget what they are called in Cocoa) and it just returned null.
    > Here is my code (assuming that global Variable is the global variable which
    > is a NSMutable Array):
    >
    > NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing", nil];
    > [globalVariable addObjectsFromArray:testing];
    >
    > Thanks for any help.
  • We *can't* assume anything. You left out the most relevant part of
    your code: how are you creating "globalVariable"?

    --
    I.S.

    On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:

    > Hi everyone:
    >
    > I am having a strange problem with NSMutableArray which is that it
    > doesn't
    > seem to work with addObjectsFromArray.  I try setting this for a
    > "global
    > variable" (forget what they are called in Cocoa) and it just
    > returned null.
    > Here is my code (assuming that global Variable is the global
    > variable which
    > is a NSMutable Array):
    >
    > NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    > nil];
    > [globalVariable addObjectsFromArray:testing];
    >
    > Thanks for any help.
  • Whoops, sorry I didn't put that in...

    @interface Example_Class : NSObject {

        IBOutlet NSTableView *tableView;
        NSMutableArray *globalVariable;
    }

    On 3/31/09 7:09 PM, "I. Savant" <idiotsavant2005...> wrote:

    >
    > We *can't* assume anything. You left out the most relevant part of
    > your code: how are you creating "globalVariable"?
    >
    > --
    > I.S.
    >
    >
    >
    > On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:
    >
    >> Hi everyone:
    >>
    >> I am having a strange problem with NSMutableArray which is that it
    >> doesn't
    >> seem to work with addObjectsFromArray.  I try setting this for a
    >> "global
    >> variable" (forget what they are called in Cocoa) and it just
    >> returned null.
    >> Here is my code (assuming that global Variable is the global
    >> variable which
    >> is a NSMutable Array):
    >>
    >> NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    >> nil];
    >> [globalVariable addObjectsFromArray:testing];
    >>
    >> Thanks for any help.
    >
  • On Wed, Apr 1, 2009 at 1:12 PM, Pierce Freeman
    <piercefreeman11...> wrote:
    > Whoops, sorry I didn't put that in...
    >
    > @interface Example_Class : NSObject {
    >
    >    IBOutlet NSTableView *tableView;
    >    NSMutableArray *globalVariable;
    > }

    :-D You still haven't put it in!

    Where do you set globalVariable to something?

    Regards,

    Chris
  • The global variable is in the Controller.h... Is this what you are asking
    for?

    On 3/31/09 7:14 PM, "Chris Suter" <csuter...> wrote:

    > On Wed, Apr 1, 2009 at 1:12 PM, Pierce Freeman
    > <piercefreeman11...> wrote:
    >> Whoops, sorry I didn't put that in...
    >>
    >> @interface Example_Class : NSObject {
    >>
    >>    IBOutlet NSTableView *tableView;
    >>    NSMutableArray *globalVariable;
    >> }
    >
    > :-D You still haven't put it in!
    >
    > Where do you set globalVariable to something?
    >
    > Regards,
    >
    > Chris
  • On 01/04/2009, at 1:12 PM, Pierce Freeman wrote:

    > @interface Example_Class : NSObject {
    >
    > IBOutlet NSTableView *tableView;
    > NSMutableArray *globalVariable;
    > }

    globalVariable isn't assigned, therefore it's null.

    --Graham
  • On Wed, Apr 1, 2009 at 1:16 PM, Pierce Freeman
    <piercefreeman11...> wrote:
    > The global variable is in the Controller.h... Is this what you are asking
    > for?

    Unless you set globalVariable to something (I do hope you haven't
    actually called it that BTW), it will remain nil forever. Messages to
    nil will yield nil.

    We're looking for something along the lines of:

      globalVariable = [[NSMutableArray alloc] init];

    Regards,

    Chris
  • You declared a pointer to an instance of NSMutableArray as an instance
    variable of Example_Class (its in no way global) there, but you didn't
    create an instance of NSMutableArray. Did you do something like this
    at some point?

    globalVariable = [[NSMutableArray alloc] init];

    - Bryan

    On Mar 31, 2009, at 10:12 PM, Pierce Freeman wrote:

    > Whoops, sorry I didn't put that in...
    >
    > @interface Example_Class : NSObject {
    >
    > IBOutlet NSTableView *tableView;
    > NSMutableArray *globalVariable;
    > }
    >
    >
    > On 3/31/09 7:09 PM, "I. Savant" <idiotsavant2005...> wrote:
    >
    >>
    >> We *can't* assume anything. You left out the most relevant part of
    >> your code: how are you creating "globalVariable"?
    >>
    >> --
    >> I.S.
    >>
    >>
    >>
    >> On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:
    >>
    >>> Hi everyone:
    >>>
    >>> I am having a strange problem with NSMutableArray which is that it
    >>> doesn't
    >>> seem to work with addObjectsFromArray.  I try setting this for a
    >>> "global
    >>> variable" (forget what they are called in Cocoa) and it just
    >>> returned null.
    >>> Here is my code (assuming that global Variable is the global
    >>> variable which
    >>> is a NSMutable Array):
    >>>
    >>> NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    >>> nil];
    >>> [globalVariable addObjectsFromArray:testing];
    >>>
    >>> Thanks for any help.
    >>

  • On Mar 31, 2009, at 9:12 PM, Pierce Freeman wrote:

    > Whoops, sorry I didn't put that in...
    >
    > @interface Example_Class : NSObject {
    >
    > IBOutlet NSTableView *tableView;
    > NSMutableArray *globalVariable;
    > }

    That's not a global variable.  It's an instance variable.  That's a
    massive conceptual error, and you should do your best to understand
    why you made it before going much further.  Perhaps reading Apple's
    guide to Object-Oriented Programming with Objective-C would help:

    http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/index.ht
    ml


    Next, what you showed is the declaration of the variable.  The
    variable is a pointer, which may point to an object.  Declaring a
    pointer does not allocate or initialize the object, nor make the
    pointer point to the object (or anything in particular).  You have to
    do that separately.

    Regards,
    Ken
  • On 01/04/2009, at 1:16 PM, Pierce Freeman wrote:

    > The global variable is in the Controller.h... Is this what you are
    > asking
    > for?

    No. Just declaring <globalVariable> doesn't actually make an instance
    of NSMutableArray. You have to write code to do that. Where is it?

    If you haven't done this, that's your problem.

    Somewhere you need:

    globalVariable = [[NSMutableArray alloc] init];

    --Graham
  • well no that's just the declaration of the NSMutableArray pointer, you
    have to actually make it somewhere, in the initializer of your
    Example_Class (and release it somewhere too)

    And that variable isn't global, it's a class instance variable.

    You might want to go back to the Objective-C 2.0 Programming Language
    chapter about classes and initialization, that whole document is very
    good and may help you sort out some ideas.

    Pierce Freeman wrote:
    > Whoops, sorry I didn't put that in...
    >
    > @interface Example_Class : NSObject {
    >
    > IBOutlet NSTableView *tableView;
    > NSMutableArray *globalVariable;
    > }
    >
    >
    > On 3/31/09 7:09 PM, "I. Savant" <idiotsavant2005...> wrote:
    >
    >
    >> We *can't* assume anything. You left out the most relevant part of
    >> your code: how are you creating "globalVariable"?
    >>
    >> --
    >> I.S.
    >>
    >>
    >>
    >> On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:
    >>
    >>
    >>> Hi everyone:
    >>>
    >>> I am having a strange problem with NSMutableArray which is that it
    >>> doesn't
    >>> seem to work with addObjectsFromArray.  I try setting this for a
    >>> "global
    >>> variable" (forget what they are called in Cocoa) and it just
    >>> returned null.
    >>> Here is my code (assuming that global Variable is the global
    >>> variable which
    >>> is a NSMutable Array):
    >>>
    >>> NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    >>> nil];
    >>> [globalVariable addObjectsFromArray:testing];
    >>>
    >>> Thanks for any help.
    >>

  • On Tue, Mar 31, 2009 at 9:16 PM, Pierce Freeman
    <piercefreeman11...> wrote:
    > The global variable is in the Controller.h... Is this what you are asking
    > for?

    @interface Foo : NSObject {
      NSMutableArray *objects;
    }

    @end

    @implementation Foo

    - (id) init {
      if (self = [super init]) {
        objects = [[NSMutableArray alloc] initWithCapacity:10]; // What
    you are missing!
      }

      return self;
    }

    @end
  • In my instance variable programming, I totally forgot that even for instance
    variables you have to do that code. :)  Sorry for any inconvenience for such
    a stupid question.

    On 3/31/09 7:17 PM, "Bryan Henry" <bryanhenry...> wrote:

    > You declared a pointer to an instance of NSMutableArray as an instance
    > variable of Example_Class (its in no way global) there, but you didn't
    > create an instance of NSMutableArray. Did you do something like this
    > at some point?
    >
    > globalVariable = [[NSMutableArray alloc] init];
    >
    > - Bryan
    >
    > On Mar 31, 2009, at 10:12 PM, Pierce Freeman wrote:
    >
    >> Whoops, sorry I didn't put that in...
    >>
    >> @interface Example_Class : NSObject {
    >>
    >> IBOutlet NSTableView *tableView;
    >> NSMutableArray *globalVariable;
    >> }
    >>
    >>
    >> On 3/31/09 7:09 PM, "I. Savant" <idiotsavant2005...> wrote:
    >>
    >>>
    >>> We *can't* assume anything. You left out the most relevant part of
    >>> your code: how are you creating "globalVariable"?
    >>>
    >>> --
    >>> I.S.
    >>>
    >>>
    >>>
    >>> On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:
    >>>
    >>>> Hi everyone:
    >>>>
    >>>> I am having a strange problem with NSMutableArray which is that it
    >>>> doesn't
    >>>> seem to work with addObjectsFromArray.  I try setting this for a
    >>>> "global
    >>>> variable" (forget what they are called in Cocoa) and it just
    >>>> returned null.
    >>>> Here is my code (assuming that global Variable is the global
    >>>> variable which
    >>>> is a NSMutable Array):
    >>>>
    >>>> NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    >>>> nil];
    >>>> [globalVariable addObjectsFromArray:testing];
    >>>>
    >>>> Thanks for any help.
    >>>

    >
  • No, I didn't call it that. ;)  And in my instance variable programming, I
    forgot that you have to run that code no matter what.

    On 3/31/09 7:17 PM, "Chris Suter" <csuter...> wrote:

    > On Wed, Apr 1, 2009 at 1:16 PM, Pierce Freeman
    > <piercefreeman11...> wrote:
    >> The global variable is in the Controller.h... Is this what you are asking
    >> for?
    >
    > Unless you set globalVariable to something (I do hope you haven't
    > actually called it that BTW), it will remain nil forever. Messages to
    > nil will yield nil.
    >
    > We're looking for something along the lines of:
    >
    > globalVariable = [[NSMutableArray alloc] init];
    >
    > Regards,
    >
    > Chris
  • Ken:

    Note to self, don't go writing to support boards when I am already half
    asleep. ;)  I realize that now, and I understand what instance variables
    are, just I couldn't think of the name when I was writing my original post.
    And in my instance variable programming, I forgot that you have to run this
    no matter if it is an instance variable or not:

    globalVariable = [[NSMutableArray alloc] init];

    On 3/31/09 7:19 PM, "Ken Thomases" <ken...> wrote:

    > On Mar 31, 2009, at 9:12 PM, Pierce Freeman wrote:
    >
    >> Whoops, sorry I didn't put that in...
    >>
    >> @interface Example_Class : NSObject {
    >>
    >> IBOutlet NSTableView *tableView;
    >> NSMutableArray *globalVariable;
    >> }
    >
    > That's not a global variable.  It's an instance variable.  That's a
    > massive conceptual error, and you should do your best to understand
    > why you made it before going much further.  Perhaps reading Apple's
    > guide to Object-Oriented Programming with Objective-C would help:
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/index.ht
    ml

    >
    > Next, what you showed is the declaration of the variable.  The
    > variable is a pointer, which may point to an object.  Declaring a
    > pointer does not allocate or initialize the object, nor make the
    > pointer point to the object (or anything in particular).  You have to
    > do that separately.
    >
    > Regards,
    > Ken
    >
  • Hey Roland:

    Yeah, thanks to yourself and others I finally remembered that you have to
    "make" the variable somewhere. ;)  And I know what a instance variable is,
    it's just that I couldn't think of the term when writing my original post.

    On 3/31/09 7:20 PM, "Roland King" <rols...> wrote:

    > well no that's just the declaration of the NSMutableArray pointer, you
    > have to actually make it somewhere, in the initializer of your
    > Example_Class (and release it somewhere too)
    >
    > And that variable isn't global, it's a class instance variable.
    >
    > You might want to go back to the Objective-C 2.0 Programming Language
    > chapter about classes and initialization, that whole document is very
    > good and may help you sort out some ideas.
    >
    > Pierce Freeman wrote:
    >> Whoops, sorry I didn't put that in...
    >>
    >> @interface Example_Class : NSObject {
    >>
    >> IBOutlet NSTableView *tableView;
    >> NSMutableArray *globalVariable;
    >> }
    >>
    >>
    >> On 3/31/09 7:09 PM, "I. Savant" <idiotsavant2005...> wrote:
    >>
    >>
    >>> We *can't* assume anything. You left out the most relevant part of
    >>> your code: how are you creating "globalVariable"?
    >>>
    >>> --
    >>> I.S.
    >>>
    >>>
    >>>
    >>> On Mar 31, 2009, at 10:05 PM, Pierce Freeman wrote:
    >>>
    >>>
    >>>> Hi everyone:
    >>>>
    >>>> I am having a strange problem with NSMutableArray which is that it
    >>>> doesn't
    >>>> seem to work with addObjectsFromArray.  I try setting this for a
    >>>> "global
    >>>> variable" (forget what they are called in Cocoa) and it just
    >>>> returned null.
    >>>> Here is my code (assuming that global Variable is the global
    >>>> variable which
    >>>> is a NSMutable Array):
    >>>>
    >>>> NSArray *testing = [NSArray arrayWithObjects:@"testing", @"testing",
    >>>> nil];
    >>>> [globalVariable addObjectsFromArray:testing];
    >>>>
    >>>> Thanks for any help.
    >>>

  • don't beat yourself up too much.

    I spent 30 minutes last night staring at this line of code and trying to
    figure out why it wouldn't compile. Checked headers, checked libraries,
    checked the definition of the function sin(). Pulled out hair by the
    handful, restarted XCode .. when it finally dawned on me I decided it
    was time to go to bed ..

    double sin = sin( angle );

    Pierce Freeman wrote:
    > Ken:
    >
    > Note to self, don't go writing to support boards when I am already half
    > asleep. ;)  I realize that now, and I understand what instance variables
    > are, just I couldn't think of the name when I was writing my original post.
    > And in my instance variable programming, I forgot that you have to run this
    > no matter if it is an instance variable or not:
    >
    > globalVariable = [[NSMutableArray alloc] init];
    >
    >
    > On 3/31/09 7:19 PM, "Ken Thomases" <ken...> wrote:
    >
    >
    >> On Mar 31, 2009, at 9:12 PM, Pierce Freeman wrote:
    >>
    >>
    >>> Whoops, sorry I didn't put that in...
    >>>
    >>> @interface Example_Class : NSObject {
    >>>
    >>> IBOutlet NSTableView *tableView;
    >>> NSMutableArray *globalVariable;
    >>> }
    >>
    >> That's not a global variable.  It's an instance variable.  That's a
    >> massive conceptual error, and you should do your best to understand
    >> why you made it before going much further.  Perhaps reading Apple's
    >> guide to Object-Oriented Programming with Objective-C would help:
    >>
    >> http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/index.ht
    ml

    >>
    >> Next, what you showed is the declaration of the variable.  The
    >> variable is a pointer, which may point to an object.  Declaring a
    >> pointer does not allocate or initialize the object, nor make the
    >> pointer point to the object (or anything in particular).  You have to
    >> do that separately.
    >>
    >> Regards,
    >> Ken
    >>

  • Thanks Roland, I probably shouldn't - It was just such a beginner's mistake.
    ;)  And as for your mistake, I probably wouldn't have gotten that ever
    figured out.

    On 3/31/09 7:45 PM, "Roland King" <rols...> wrote:

    > don't beat yourself up too much.
    >
    > I spent 30 minutes last night staring at this line of code and trying to
    > figure out why it wouldn't compile. Checked headers, checked libraries,
    > checked the definition of the function sin(). Pulled out hair by the
    > handful, restarted XCode .. when it finally dawned on me I decided it
    > was time to go to bed ..
    >
    > double sin = sin( angle );
    >
    > Pierce Freeman wrote:
    >> Ken:
    >>
    >> Note to self, don't go writing to support boards when I am already half
    >> asleep. ;)  I realize that now, and I understand what instance variables
    >> are, just I couldn't think of the name when I was writing my original post.
    >> And in my instance variable programming, I forgot that you have to run this
    >> no matter if it is an instance variable or not:
    >>
    >> globalVariable = [[NSMutableArray alloc] init];
    >>
    >>
    >> On 3/31/09 7:19 PM, "Ken Thomases" <ken...> wrote:
    >>
    >>
    >>> On Mar 31, 2009, at 9:12 PM, Pierce Freeman wrote:
    >>>
    >>>
    >>>> Whoops, sorry I didn't put that in...
    >>>>
    >>>> @interface Example_Class : NSObject {
    >>>>
    >>>> IBOutlet NSTableView *tableView;
    >>>> NSMutableArray *globalVariable;
    >>>> }
    >>>
    >>> That's not a global variable.  It's an instance variable.  That's a
    >>> massive conceptual error, and you should do your best to understand
    >>> why you made it before going much further.  Perhaps reading Apple's
    >>> guide to Object-Oriented Programming with Objective-C would help:
    >>>
    >>>
    http://developer.apple.com/documentation/Cocoa/Conceptual/OOP_ObjC/index.ht
    m
    >>>
    l
    >>>
    >>> Next, what you showed is the declaration of the variable.  The
    >>> variable is a pointer, which may point to an object.  Declaring a
    >>> pointer does not allocate or initialize the object, nor make the
    >>> pointer point to the object (or anything in particular).  You have to
    >>> do that separately.
    >>>
    >>> Regards,
    >>> Ken
    >>>

  • Hey Graham :

    Yeah, I finally figured that out. ;) It now works like a charm.

    On 3/31/09 7:20 PM, "Graham Cox" <graham.cox...> wrote:

    >
    > On 01/04/2009, at 1:16 PM, Pierce Freeman wrote:
    >
    >> The global variable is in the Controller.h... Is this what you are
    >> asking
    >> for?
    >
    >
    > No. Just declaring <globalVariable> doesn't actually make an instance
    > of NSMutableArray. You have to write code to do that. Where is it?
    >
    > If you haven't done this, that's your problem.
    >
    > Somewhere you need:
    >
    > globalVariable = [[NSMutableArray alloc] init];
    >
    >
    >
    > --Graham
    >
    >
  • On Wed, Apr 1, 2009 at 10:33 AM, Pierce Freeman
    <piercefreeman11...> wrote:

    > Yeah, I finally figured that out. ;) It now works like a charm.

      Just for completeness, the plain-language concept to remember is:

    // "Let there be an NSMutableArray pointer named 'globalVariable'."
    NSMutableArray * globalVariable;

    ... and ...

    // "Create an NSMutableArray instance and assign it to the
    'globalVariable' pointer
    // ... so when I talk to 'globalVariable', I mean this instance of
    NSMutableArray."
    globalVariable = [[NSMutableArray alloc] init];

      By contrast, when you're creating objects inside a method (temporary
    objects whose scope is limited to that method), do this all in one go:

    NSMutableArray * myTemporaryArray = [[NSMutableArray alloc] init];

      - or, more simply -

    NSMutableArray * myTemporaryArray = [NSMutableArray array];

      ... then you'll use it then let it die, or hand it off to someone else.

      However, since you're creating an instance variable in your class,
    you declare the pointer in your header and then create an array and
    assign it to the pointer somewhere in your implementation. The most
    likely place (arguably by best practice) would be the class's -init
    method, since you want a mutable array ready for use when an instance
    of your class is created (allocated and initialized).

      I hope that helps a bit.

    --
    I.S.
  • Yeah, it helps a lot... And hopefully anyone that has this problem will look
    at this before writing to any support site. :)

    On 4/1/09 8:06 AM, "I. Savant" <idiotsavant2005...> wrote:

    > On Wed, Apr 1, 2009 at 10:33 AM, Pierce Freeman
    > <piercefreeman11...> wrote:
    >
    >> Yeah, I finally figured that out. ;) It now works like a charm.
    >
    > Just for completeness, the plain-language concept to remember is:
    >
    > // "Let there be an NSMutableArray pointer named 'globalVariable'."
    > NSMutableArray * globalVariable;
    >
    > ... and ...
    >
    > // "Create an NSMutableArray instance and assign it to the
    > 'globalVariable' pointer
    > // ... so when I talk to 'globalVariable', I mean this instance of
    > NSMutableArray."
    > globalVariable = [[NSMutableArray alloc] init];
    >
    > By contrast, when you're creating objects inside a method (temporary
    > objects whose scope is limited to that method), do this all in one go:
    >
    > NSMutableArray * myTemporaryArray = [[NSMutableArray alloc] init];
    >
    > - or, more simply -
    >
    > NSMutableArray * myTemporaryArray = [NSMutableArray array];
    >
    > ... then you'll use it then let it die, or hand it off to someone else.
    >
    > However, since you're creating an instance variable in your class,
    > you declare the pointer in your header and then create an array and
    > assign it to the pointer somewhere in your implementation. The most
    > likely place (arguably by best practice) would be the class's -init
    > method, since you want a mutable array ready for use when an instance
    > of your class is created (allocated and initialized).
    >
    > I hope that helps a bit.
    >
    > --
    > I.S.
previous month april 2009 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today