NSLog displaying (null)


#1

Hello. I’m working through the first exercise and seeing a little weirdness.
Part of the weirdness is that I’m running XCode 4.5.2 and Storyboards are a little different than the examples in the book. I managed to make connections just by poking around, clicking, dragging. My final results looked like the screen shots in the book.

Where I’m running into trouble: Every time I click my Show Question button, NSLog tells me:
2012-11-26 13:51:11.965 Quiz[13259:c07] displaying question: (null)

Tried everything I can think of to solve this challenge, to no avail. It looks like my questions array isn’t being created. Any thoughts?

My code follows:

@implementation QuizViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    //call the init method implemented by the superclass
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self){
        
        //create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        
        [questions addObject:@"What is 7+7?"];
        [answers addObject:@"14"];
        
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
    }
    
 
    return self;

}

-(IBAction)showQuestion:(id)sender
{
    //step to the next question
    currentQuestionIndex++;
    
    //am i past the last question?
    if(currentQuestionIndex == [questions count]){
        
        //go back to first question; set question index to 0
        currentQuestionIndex = 0;
    }
    
    //get the string at that index in the questions array
    NSString *question = [questions objectAtIndex:currentQuestionIndex];
    
    //log th string to the console
    NSLog(@"displaying question: %@", question);
    
    //display the string in the question field
    [questionField setText];
    
    //clear the answer field
    [answerField setText:@"???"];
    
}

-(IBAction)showAnswer:(id)sender
{
   //What is the answer to the current question?
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];
    [answerField setText:answer];
}

@end

#2

Did you check that initWithNibName:bundle: method is being called? Because that’s where the questions array is being created.

Add some logs to check:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog (@"---> %s", __PRETTY_FUNCTION__);

    //call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self){
    ...
    }
    NSLog (@"---> %s: %@", __PRETTY_FUNCTION__, questions);

    return self;
}

Also if questions and answers are properties, you should initialise them like this:

    if(self){
        //create two arrays and make the pointers point to them
        self.questions = [[NSMutableArray alloc] init];
        self.answers = [[NSMutableArray alloc] init];
        ...
     }

You should also print the contents of the questions array to see if it contains anything before accessing it.


#3

[quote=“ibex10”]Did you check that initWithNibName:bundle: method is being called? Because that’s where the questions array is being created.

I did - and I tried again using the code provided.
No output appears in the console, meaning initWithNibName isn’t being called.

According to the text, “When the application launches, the QuizViewController will be sent the message initWithNibName:bundle:. In QuizViewController.m, implement the initWithNibName:bundle: method by adding the following code that creates two arrays and fills them with questions and answers.”

I’m kinda new at this; any idea as to why it’s not being called?


#4

I had the same problem. I moved the code to initialise the arrays into the viewDidLoad() method and deleted the initWithNibName() method.


#5

Hi All,

I’m new too on iOS programming and I was stuck (already) with the first pages of the book… :cry:

I checked on google and also on the forum more deep… It is because when you started the new project your made it with the option StoryBoard (the page you give a name to the project).
I started the project again and unselect the [] StoryBoard button and it works perfectly !

Now, for the people who did the StoryBoard version and want to keep it like that… I dont have a clue to how to change the code…


#6

Hey, thank you! I made a new project, unchecked the storyboard checkbox, brought in the right bits of code from the last one, and it all works! THANK YOU AGAIN!


#7

Wow, unchecking the storyboard option fixed me up, too.
I wonder how anyone could ever find such a thing if it weren’t Googleable code from a book. If I’d just made that error on my own, as far as I know I’d get old without ever knowing where to look.


#8

In Xcode 5, where is the checkbox to uncheck the storyboard and use .xib files instead of the storyboard?

Thanks,


#9

[quote=“jfletcher”]In Xcode 5, where is the checkbox to uncheck the storyboard and use .xib files instead of the storyboard?

Thanks,[/quote]

I would also like to know this. Same problem as people above.


#10

If you’re coding with a storyboard, the initWithNibName:bundle doesn’t seem to be called when the application loads.

Instead of following the book’s advice to delete the boilerplate code, you can use the given function viewDidLoad to run code when the application loads.

On my screen, I have the existing comment:

and wrote the array creation code (page 18) underneath this comment:

questions = [[NSMutableArray alloc] init]; ... [answers addObject:@"Grapes"]