0

I'm really frustrated after spending like three hours googling around to solve this problem!! It's probably an easy solution to it aswell.

I'm creating a really simple TableView app for the iPhone. It's supposed to fetch an XML-document and parse it (already fixed) and then put the data into objects called HPobject! One HPobject represents one day of data from the XML-file. Anyhow!

Then I want the object to be stored in a NSMutableArray so I can grab it later when I'm creating the table rows. But I can't access it! My NSMutableArray is ALWAYS null! No matter what I do!

Here's my code:

//THE .h FILE
#import "TBXML.h"
#import "HPobject.h"

@interface RootViewController : UITableViewController {
    NSMutableArray *listOfItems;
}

- (void)traverseElement:(TBXMLElement *)element;

//THE .m FILE
#import "RootViewController.h"
#import "HPobject.h"

@implementation RootViewController

- (void)viewDidLoad
{   
    NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
    TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.hpprov.se/istasts.php?q=xxxxxxx"]] retain];

    if (tbxml.rootXMLElement)
        [self traverseElement:tbxml.rootXMLElement]; //Works fine!

    [tbxml release];
    [super viewDidLoad];
}

- (void) traverseElement:(TBXMLElement *)element {    
    do {
//Lots of parsing code which all works fine and gets me the variables up next!

            HPobject *currentObject = [[HPobject alloc] init];
            currentObject.antalRegistrerade = numRegistrerade;
            currentObject.inkomstBrutto = numBrutto;
            currentObject.inkomstNetto = numNetto;

            [listOfItems addObject:currentObject];

            NSLog(@"Array: %@", listOfItems); //RETURNS null!
            NSLog(@"Reg: %@, Net: %@, Brutt: %@", currentObject.antalRegistrerade, currentObject.inkomstNetto, currentObject.inkomstBrutto); //Returns the correct values!

            NSLog(@"%d stycken!", listOfItems.count); //Returns 0!! :(
            [currentObject release];

    } while ((element = element->nextSibling));  
}
1
  • Why does is return NULL?!?? :( And why is the count 0?? Commented Apr 14, 2011 at 10:12

3 Answers 3

3

You are defining listOfItems locally in viewDidLoad and then you try to access that in another method.

Make sure you are using an instance variable defined in your interface definition (header).

replace this line in viewDidLoad:

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

with

listOfItems = [[NSMutableArray alloc] init];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply! And... how do I do that? I know I should read a book on Objective-C and its fantastic implementation, and I will, but for now I need a quick answer! Thank you!
Even better would be to use a (synthesized) getter/setter: self.listOfItems = [NSMutableArray array];
See my solution - simply remove "NSMutableArray *" from viewDidLoad.
2

I suppose that listOfItems is an ivar. So to fix your problem change this:

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

to this

listOfItems = [[NSMutableArray alloc] init];

1 Comment

As a rule, you should use instance variables starting with an underscore character. That makes it much more obvious that you are assigning to an instance variable.
1

Take a look at the scope of your array. You have created that as a variable of another method. It will not visible in others. Make an instance var.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.