Problems... can't figure it out


Ok, I’f been doing well with the book, and all my questions about any compiler errors have bee solved with errata and other members of this forum…

But I was on page 197 of the book and I went to build and rum my app. I am using the simulator at the moment because I don’t have the full dev license yet.

I go through the app and click on the first possession created, that goes into the edit view. I click on the camera button on the nav bar, that goes into the photo albums, I click the default album, then click the first picture, and an exception is thrown…

I’m really confused as to why… I’ve checked my code so far and I think it’s just as the book has said it was. And I even set breakpoints and tried to follow the flow and see where the error is occuring…


here’s the error in the console - it seems that nil was being placed into an imageKey and I don’t know where that was happening…:

2010-10-31 15:01:11.722 HomePwner[3136:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: 103148B1-3366-4A5F-A733-61EDAB37D7DB)'

I’ll post my code in the next post.



// ItemDetailViewController.m
// HomePwner
// Created by David Scott on 10/18/10.
// Copyright 2010 Student. All rights reserved.

#import “ItemDetailViewController.h”
#import “Possession.h”
#import “ImageCache.h”

@implementation ItemDetailViewController

@synthesize editingPossession;

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
    // Custom initialization
    return self;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

  • (void)viewDidLoad {
    [super viewDidLoad];

// Override to allow orientations other than the default portrait orientation.

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

  • (id)init
    [super initWithNibName:@“ItemDetailViewController” bundle:nil];

    //create a UIBarButtonItem with a camera icon, will send
    //takePicture: to out ItemDetailViewController when tapped
    UIBarButtonItem *cameraBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
    //Place this image on our navigation bar when this viewcontroller
    //is on top of the navigation stack
    [[self navigationItem]setRightBarButtonItem:cameraBarButtonItem];

    //cameraBarButtom is retained by the navigation item
    [cameraBarButtonItem release];
    return self;

  • (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
    return [self init];

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

//if our device has a camera, we want to take a picture, otherwise, we
//just pick from the photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
	[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}else {
	[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//image picker needs a delegate so we can respond to its messages
[imagePicker setDelegate:self];

//Place image picker on the screen
[self presentModalViewController:imagePicker animated:YES];

//the image picker will be retained by the ItemDetailViewController
//until it has been dismissed
[imagePicker release];


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *oldKey = [editingPossession imageKey];

//did the possession already have an image?
if (oldKey) {
	//delete the old image
	[[ImageCache sharedImageCache] deleteImageForKey:oldKey];

//get picked image from into dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//create a CFUUID object - it knows how to create unique identifiers
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);

//create a string from unique identifier
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);

//Use that unique ID to set our possessoins imageKey
[editingPossession setImageKey:(NSString *)newUniqueIDString];

//we used "create" in the functions to make objects, we need to release them

//store image in the ImageCache with this key
[[ImageCache sharedImageCache] setImage:image forKey:[editingPossession imageKey]];

//put that image into the screen in our image view
[imageView setImage:image];

//take image picker off the screen - 
//you must call this dismiss method
[self dismissModalViewControllerAnimated:YES];


[super viewWillDisappear];

//clear first responder
[nameFeild resignFirstResponder];
[serialNumberFeild resignFirstResponder];
[valueFeild resignFirstResponder];

//"save" changes to editingPossession
[editingPossession setPossessionName:[nameFeild text]];
[editingPossession setSerialNumber:[serialNumberFeild text]];
[editingPossession setValueInDollars:[[valueFeild text] intValue]];


[super viewWillAppear];

[nameFeild setText:[editingPossession possessionName]];
[serialNumberFeild setText:[editingPossession serialNumber]];
[valueFeild setText:[NSString stringWithFormat:@"%d", [editingPossession valueInDollars]]];

//create a NSDateFormatter that will turn a date into a simple date string
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

//Use filtered NSDate object to set dateLabel contents
[dateLabel setText:[dateFormatter stringFromDate:[editingPossession dateCreated]]];

//change the navigation item to display name of possession
[[self navigationItem] setTitle:[editingPossession possessionName]];

NSString *imageKey = [editingPossession imageKey];

if (imageKey) {
	//get image for imageKey from image cache
	UIImage *imageToDisplay = [[ImageCache sharedImageCache]imageForKey];
	//use that image to put on the screen in imageView
	[imageView setImage:imageToDisplay];
}else {
	//clear the imageView
	[imageView setImage:nil];


  • (void)didReceiveMemoryWarning {
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren’t in use.

  • (void)viewDidUnload {
    [super viewDidUnload];

    [nameFeild release];
    nameFeild = nil;

    [serialNumberFeild release];
    serialNumberFeild = nil;

    [valueFeild release];
    valueFeild = nil;

    [dateLabel release];
    dateLabel = nil;

    [imageView release];
    imageView = nil;

  • (void)dealloc {
    [nameFeild release];
    [serialNumberFeild release];
    [valueFeild release];
    [dateLabel release];
    [imageView release];
    [super dealloc];


If someone could help me out, it would be MUCH appreciated!



Okay, so I downloaded a picture to the simulator from the internet that was a smaller size, and it worked! I guess it was the same issue the other person on the forum was having that had the same problem, lol.