Reputation: 967
I did the "Build and analyze" in xCode and get "Dereference of null pointer" . I noted in my code below for which row I get the message. I'm developing for iPhone.
"PDFScrollView.h"
#import "ENsightAppDelegate.h"
@interface PDFScrollView : UIScrollView <UIScrollViewDelegate> {
ENsightAppDelegate *appDelegate;
}
@end
"PDFScrollView.m"
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Set up the UIScrollView
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
//self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
[self setBackgroundColor:[UIColor grayColor]];
self.maximumZoomScale = 5;//200
self.minimumZoomScale = 0.5;//.85
self.userInteractionEnabled = YES;
self.delaysContentTouches = YES;
self.canCancelContentTouches = NO;
}
appDelegate =(ENsightAppDelegate *) [[UIApplication sharedApplication] delegate];
pdfView=[appDelegate GetLeaveView];
[self addSubview:pdfView];
return self;
}
It's not the complete code, pasted what I think is useful.
I find this quite strange. How come I get this message?
Upvotes: 2
Views: 2110
Reputation: 17143
If self is nil, then you can't access instance variables of 'self'. So you should reference those variables only inside of the if statement. In other words, only if self is not nil.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Set up the UIScrollView
self.showsVerticalScrollIndicator = NO;
// ... bunch of other properties set...
// ... btw, better style here would be to set the ivars directly (in an initializer)
appDelegate =(ENsightAppDelegate *) [[UIApplication sharedApplication] delegate];
pdfView=[appDelegate GetLeaveView];
[self addSubview:pdfView];
}
return self;
}
Upvotes: 8
Reputation: 137322
appDelegate
is actually [self appDelegate]
, so if you assign null to self
, you dereference null pointer.
Anyway - self
points to the current object, why would you assign to it? You can use it directly without an assignment.
And last thing - you do use self
explicitly in the on-before-last line anyway, and here it is clear that it is null:
[self addSubview:pdfView];
Upvotes: 0
Reputation: 2227
is appDelegate
an instance var of the class? if so, you should be doing it in the if (self =....
. same with the [self addSubview...]
. basically if that if statement fails, you don't have an object to work with.
Upvotes: 1