Search code examples
iphoneobjective-cnsarraynslog

Why is this NULL?


Can anyone tell me why this is NULL ?

So Array1 is NSMutable, and has an NSString object "UserName" at index 0;

NSLog (@"Contents of array1 %@", [array1 objectAtIndex:0]); //prints UserName

Now , I do this...

array2 belongs to another class. I reference the class, import .h file, and add property and synthesize it.

[object2.array2  addObject: array1]; //array2 is NSMutable properly initialized in it's respective class in the init method

NSLog (@"Contents of array2 %@", [object2.array2 objectAtIndex:0]); //prints (null)

tempArray = [[NSMutableArray alloc] init];
tempArray = [object2.array2 objectAtIndex:0];
NSLog (@"%@", [tempArray objectAtIndex:0]); // Prints (null) // Should be UserName

Adding more information:

object2 is an object of another class (which is a subclass of NSObject), and object2 contains array2 as it's property.

object2 is nil. WHY ? HOW ??


Solution

  • NSLog (@"Contents of array2 %@", [object2.array2 objectAtIndex:0]); //prints NULL
    

    If that 'prints NULL', then it is for one of four reasons:

    • object2 is nil
    • object2.array2 returns nil
    • the object at index 0 is the string "NULL"
    • the object at index 0's description method returns the string "NULL"

    Which is it?


    object 2 is nil. I wonder why.

    Most likely because you didn't initialize it in the first place. Show your initialization code and also drop a log statement or breakpoint and make sure it is actually being executed.