0

I'm having a problem with this code here

Book *theNewBook = [self parseTheBookXML];
// The book is not nil here
NSLog(@"The book's title: %@, number of pages:%@ and author: %@",theNewBook.title, theNewBook.pages, theNewBook.author);

[_theBooksArray addObject:theNewBook];

// TEST
Book *testBook = [_theBooksArray objectAtIndex:0];
// The book is nil here
NSLog(@"The book's title: %@, number of pages:%@ and author: %@",testBook.title, testBook.pages, testBook.author);

Can anyone tell me why my book object is 'nil' because I've hit a wall over here...

Groot
  • 13,943
  • 6
  • 61
  • 72

3 Answers3

1

As you can see from the comments on my question my problem was that I had not initialized the array I was accessing. So INSTEAD of

[_theBooksArray addObject:theNewBook];

calling

_theBooksArray = [[NSMutableArray alloc] initWithObjects:theNewBook, nil];

will do the trick.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Groot
  • 13,943
  • 6
  • 61
  • 72
0
[_theBooksArray addObject:theNewBook];
[_theBooksArray count];

print the log and check the array count.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Saurabh Shukla
  • 1,368
  • 3
  • 14
  • 26
  • The OP already solved the problem by checking if the array was initialized. – David Rönnqvist Jan 24 '13 at 13:27
  • @AKV At the time of writing, he has 2 downvotes and 1 upvote, so he would lose 6 points if he deleted his answer. – trojanfoe Jan 24 '13 at 14:29
  • @trojanfoe :p I like downvotes more than up votes, but there should a comment with that. from past 15 days.... i m gettng downvotes without any comment. Even I cant know who is downvoting me 3-5 times everyday. :( – Anoop Vaidya Jan 24 '13 at 17:19
0

You Must Init The Array Before Using it.

_theBooksArray = [[NSMutableArray alloc] init];

Hmeedo
  • 16