Displaying question: (null)


I am trying to learn iOS programming from the 3rd edition VERY quickly on account of I am expected to write a fully functioning legit iPad app for NASA by the end of my internship program in August, so you have no idea how much any help is appreciated, particularly a quick reply! Thanks in advance:

I can’t find any inconsistencies with the code in the book but I have no idea what I’m doing wrong that is causing the question to be null. I am very new to iOS so it may be obvious but I am totally at a loss. If someone could figure out what I’m doing wrong I would be infinitely appreciative - I have spent the entire workday on this - but I have a feeling it may have something to do with the fact that I’m using xcode 4.3.2 with iOS 5.1 and thus have storyboards rather than xib files in the Single View Application.

My code for QuizViewController.m

@interface QuizViewController ()


@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];
        //Add questions and answers to the arrays
        [questions addObject:@"What is the name of Batman's sidekick?"];
        [answers addObject:@"Robin"];
        [questions addObject:@"Who is Batman's arch nemesis?"];
        [answers addObject:@"The Joker"];
        [questions addObject:@"Who is the stupidest character in The Dark Knight Rises?"];
        [answers addObject:@"Catwoman"];
    return self;

- (IBAction)showQuestion:(id)sender
    //Step to the next question
    //Am I past the last question?
    if (currentQuestionIndex == [questions count]) {
        //Go back to the first question
        currentQuestionIndex = 0;
    //Get the string at that index in the questions array
    NSString *question = [questions objectAtIndex:currentQuestionIndex];
    //Log the 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];
    //Display it in the answer field
    [answerField setText:answer];


Code for QuizViewController.h

@interface QuizViewController : UIViewController
    int currentQuestionIndex;
    //the model objects
    NSMutableArray *questions;
    NSMutableArray *answers;
    //the view objects
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;

- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;



Since you haven’t received a reply by someone yet, I’ll try to do what I can, although I don’t have your answer.

When you say that the question is null, how do you know this? At what point do you see the null value?

It’s been a month or so since I’ve been in the book/chapter that you’re in, so I don’t remember all the details, but I’m curious about you saying that you don’t have XIB files since you’re using xcode 4.3.2 and ios 5.1. I’m using the same setup, and in looking back at my quiz project, I have an xib file in there.

Here are a couple steps I would take to start figuring out where things are breaking down.

  1. I would first set a breakpoint in the (IBAction)showQuestion:(id)sender method next to the line: NSLog(@“displaying questions…” The way you do that, if you don’t know, is to click in the light gray bar next to the coding window next to the line where you want to set the break point.

  2. Then I would run the program. Click on the Show Question button and see if this causes a break in the program. It should pop up the xcode window, because it has paused next to the NSLog line. In the debug area, I believe this should be at the bottom of your screen, you should see 4 line items. One of them has a blue L next to it with the word “question” in bold. In a perfect world, you would see @“What is the nameof Batman’s sidekick?” at the end of that line. Then you would know that the question, in fact, is not null. Or, you would see the null value there.

  3. If it IS null, or if the break never happened, then you probably have some problem between tying the “Show question” button to the action.

  4. If I needed to go further back, in looking for a place where there is some success, so you can start bracketing the problem, I would put an NSLog statement after you MutableArray was being populated.

for example:

[questions addObject:@"Who is Batman's arch nemesis?"];
        [answers addObject:@"The Joker"];
        [questions addObject:@"Who is the stupidest character in The Dark Knight Rises?"];
        [answers addObject:@"Catwoman"];

    NSLog(@"Questions array: %@", questions);
    NSLog(@"Answers array: %@", answers);
    return self;

I’m guessing that there won’t be any problems there, but at least you’d have a definite starting point. Then I’d keep using either break points and/or NSLog statements to track down where things are coming unraveled. Being that you’re at the beginning of the book, though, I’d definitely figure out how to get those xib files. You’ll be needing them.

Sorry that I couldn’t help solve your problem a bit more, but there wasn’t a lot to go on.

Oh yeah, to remove the break point, click & drag the breakpoint to the left away from the gray bar it was located in and release it.


WOW I’m a total dingus I realized it’s because I checked the box for “Use Storyboards” when I shouldn’t have. When you use storyboards it replaces the xib file with a storyboard. That’s what I get for intentionally not following directions–an entire day and a half wasted. -_-

Sorry about that, thanks so much though. I was worried that I was screwed by the new versions. False alarm! Sorry y’all :smiley: