user3743552
user3743552

Reputation: 141

[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]

I have EAGLView hodling presentFrameBuffer and saving the screenshot from EAGLView send this sreenshot to UIViewController for UIActivityViewController social network framework. So, i saved in NSUserDefaults then retrieve in UIViewController. But i'm getting Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

EAGLView.h

@property (nonatomic, copy) UIImage * screensht;

   @property (nonatomic, copy) UIButton * save;

EAGLview.mm

- (BOOL)presentFramebuffer
 {


 if (_takePhotoFlag)

 {


 UIImage *glImage = [self glToUIImage];
 self.screensht = [self createSavableImage:glImage];

  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(self.screensht) forKey:@"image"];

 UIImageWriteToSavedPhotosAlbum(self.screensht, nil, nil, nil);

 }

 glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);

 return [context presentRenderbuffer:GL_RENDERBUFFER];
 }

 -(void)save:(id)sender{


 _takePhotoFlag = YES;

 }

Viewcontroller.mm:

- (void)viewDidLoad
 {
 [super viewDidLoad];

 [eaglView.save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];

 }



 -(void)save:(id)sender{


 NSLog(@"save click");

 NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
 UIImage* image = [UIImage imageWithData:imageData];



 NSArray *activityItems = @[image];

 UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
 avc.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, nil];
 [self presentViewController:avc animated:YES completion:nil];



 }

NSUserDefaults Null:

 [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(self.screensht) forKey:@"image"];

         NSLog(@"nsuserdefauls is %@",[[NSUserDefaults standardUserDefaults] stringForKey:@"image"]);

nsuserdefauls is (null)

Upvotes: 2

Views: 9400

Answers (2)

user1709076
user1709076

Reputation: 2856

For me the answer was that I was calling

if([_something respondsToSelector:@selector(objectForKey:)])
{
   id a = [_something objectForKey:@"someKey"];
}

but '_something' was 'nil' and that's where I was getting the crash

Upvotes: -2

Paulw11
Paulw11

Reputation: 114992

You are storing your image as an NSData object, but trying to retrieve it using stringForKey - this will give you nil because the "image" key does not contain a string. You should use

NSData *imageData=[[NSUserDefaults standardUserDefaults] objectForKey:@"image"]);
UIImage *image=[UIImage imageWithData:imageData];

Upvotes: 0

Related Questions