0
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (urlIndex == maxIndex) { 
    maxIndex = maxIndex + 1;

    NSString* sURL = webpage.request.URL.absoluteString;

    [urlHistory addObject:sURL];
    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);


    [sURL release];

    urlIndex = urlIndex + 1;
}
else {
    [urlHistory insertObject:webView.request.URL.absoluteString atIndex:(urlIndex - 1)];
}
}

This line

    NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);

prints (null), while as this line

    NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);

prints the actual URL.

On my initWithNibName I have:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"Tab1ViewController" bundle:nil];

if (self) {
    urlHistory = [[NSMutableArray alloc] init];
    urlIndex = 0;
    maxIndex = 0;
}

return self;
}

But I still keep getting (null) when I access my array. Why is that?

3
  • 1
    Is that a typo in your question, you are adding objects to urlIndex (presumably an integer) and not urlHistory? Commented Oct 31, 2011 at 22:03
  • Yeah fixed that thanks, but that was on an irrelevant line tho... Commented Oct 31, 2011 at 22:07
  • 1
    There are no irrelevant lines :) Commented Oct 31, 2011 at 22:08

1 Answer 1

6

It sounds like your urlHistory object is nil. Calling -objectAtIndex: on a nil object will return nil. You probably forgot to initialize urlHistory in your -init, or you're not actually calling the -init method you think you are (e.g. if your VC is loaded from a nib it will be using -initWithCoder: instead of -initWithNibName:bundle:).

For the record, if -objectAtIndex: on an NSArray ever returns nil, it means the array itself is nil. Since NSArray cannot store nil, -objectAtIndex: with a valid index will never return nil, and -objectAtIndex: with an invalid index will throw an exception. So the only way for -objectAtIndex: to return nil is if the method itself is never actually called.

Sign up to request clarification or add additional context in comments.

7 Comments

@Gibson: I can't really answer that without access to all of your code, but you can verify that this is what's going on by simply logging out NSLog(@"urlHistory: %@", urlHistory). You should see urlHistory: (null) logged out.
@gibson, are you definitely creating the controller using initWithNibName? Have you logged in that method?
@Gibson: If your view controller is being created inside a nib (e.g. the VC itself is in a nib) then it uses -initWithCoder: instead of -initWithNibName:bundle:.
@jrturton: Yeah, I saw his comment saying that too, which is why I didn't suggest that he had the wrong init method.
@KevinBallard - just update your answer, it's way better than mine and an extra one would just be noise.
|

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.