NSInvalidArgumentException', reas-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[4]'

37,455

Solution 1

The error means you are trying to put nil in the dictionary (which is not allowed). Since you are building the dictionaries with string literals those can't be nil. This means the problem is with one or more of your images.

Try this to help find the problem:

+(NSArray *)users
{
    UIImage *image1 = [UIImage imageNamed:@"person1.jpeg"];
    UIImage *image2 = [UIImage imageNamed:@"person2.jpeg"];
    UIImage *image3 = [UIImage imageNamed:@"person3.jpeg"];
    UIImage *image4 = [UIImage imageNamed:@"person4.jpeg"];

    NSDictionary *user1 = @{@"username" : @"master photographer", @"email" : @"[email protected]", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : image1 };
    NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"[email protected]", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : image2 };
    NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"[email protected]", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : image3 };
    NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"[email protected]", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : image4 };

    NSArray *userArray = @[user1, user2, user3, user4];
    return userArray;
}

Now you can either use the debugger and see if image1, image2, image3, or image4 is nil or add NSLog statements for each.

Keep in mind that filenames are case sensitive so be sure the name you pass to imageNamed: exactly matches the real filename. Also verify the images have the extension jpeg and not jpg. Make sure the images are being packaged in your resource bundle.

Solution 2

One of the UIImages you're trying to create are nil. Please add the following code as the first line of the method +(NSArray *)users

NSAssert([UIImage imageNamed:@"person1.jpeg"] != nil, @"Person 1 image does not exist");
NSAssert([UIImage imageNamed:@"person2.jpeg"] != nil, @"Person 2 image does not exist");
NSAssert([UIImage imageNamed:@"person3.jpeg"] != nil, @"Person 3 image does not exist");
NSAssert([UIImage imageNamed:@"person4.jpeg"] != nil, @"Person 4 image does not exist");

That snippet of code will print on console what of the images does not exist. Run please on DEBUG and let's see what the result of the assertions is.

Hope this helps!

Solution 3

The better cure probably is to make the "NSDictionary" like this:

NSDictionary *user1 = [NSDictionary dictionaryWithObjectsAndKeys: my obj & key here, nil];

Solution 4

"attempt to insert nil object from objects[4]" means that either the key or value at index [4] is nil.

So as the others have said it would be one of your images since the key at index [4] is a string literal.

Solution 5

It means that one of the values you're trying to put in that dictionary is nil, most likely one of the pictures. You might try assigning the images to variables and inspecting them to see if this is the case.

Share:
37,455

Related videos on Youtube

zramled
Author by

zramled

Updated on October 10, 2020

Comments

  • zramled
    zramled over 3 years

    i got error NSInvalidArgumentException this is my model class

    +(NSArray *)users
    {
        NSDictionary *user1 = @{@"username" : @"master photographer", @"email" : @"[email protected]", @"password" : @"drowssap", @"age" : @24, @"profilePicture" : [UIImage imageNamed:@"person1.jpeg"]};
        NSDictionary *user2 = @{@"username" : @"Lots of tots", @"email" : @"[email protected]", @"password" : @"icecreamrocks", @"age" : @65, @"profilePicture" : [UIImage imageNamed:@"person2.jpeg"]};
        NSDictionary *user3 = @{@"username" : @"iTechie", @"email" : @"[email protected]", @"password" : @"infiniteloop", @"age" : @30, @"profilePicture" : [UIImage imageNamed:@"person3.jpeg"]};
        NSDictionary *user4 = @{@"username" : @"Royal", @"email" : @"[email protected]", @"password" : @"IGotAPalace", @"age" : @0, @"profilePicture" : [UIImage imageNamed:@"person4.jpeg"]};
    
        NSArray *userArray = @[user1, user2, user3, user4];
        return userArray;
    }
    
    @end
    

    and this is my viewdidload

    self.users = [DMZUserData users];
    NSLog(@"%@", self.users);
    
    • rmaddy
      rmaddy about 10 years
      One (or more) of your images doesn't exist resulting in nil.
    • trojanfoe
      trojanfoe about 10 years
      I think the error message tells you what's wrong. Anything in that list of dictionary values that could potentially be nil is nil. Do some debugging and find it yourself.
    • matt
      matt about 10 years
      Did you have a question?
    • zramled
      zramled about 10 years
      @maddy i had an image file on my left side in xcode..
    • zramled
      zramled about 10 years
      @trojanfoe sorry bro but unfortunately i am currently studying in objective c for now so i am very noob.. i am just ask for your help on how to fix this error i spent more than a half day about this error :(
    • trojanfoe
      trojanfoe about 10 years
      No problem. Objective-C collection classes (including NSDictionary) cannot contain nil values. Basically one of your image files is not being packaged into your app bundle. To debug create a separate NSImage of each and run the code in a debugger to ensure they aren't nil.
    • zramled
      zramled about 10 years
      @trojanfoe very helpful..it a bit confusing when I drag my image file because i drag it several and similar name and the error is the file image.its not jpeg and the right its jpg..thank you for your quickly response..thank very much.. -SOLVED :)
  • Léo Natan
    Léo Natan about 10 years
    Also worth noting that, if an Xcode asset catalog is used, the extension should be omitted. Since the images are jpeg, this is likely not the case, but should still be mentioned.
  • Tash Pemhiwa
    Tash Pemhiwa about 9 years
    How is this a better cure?
  • Mike97
    Mike97 about 9 years
    Simply because this method: + (id) dictionaryWithObjectsAndKeys:(id)firstObject, ... return an exception for nil key... istead of: - (id) initWithDictionary:(NSDictionary*)dictionary copyItems:(BOOL)flag (I think this method is called for the example here)...that appear to return nothing about nil keys