Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Message not sending in Parse! (Objective-C)

Hey Everyone,

In Parse I have a class called 'CustomMessage' which carries messages sent by the user via the iPhone app. It was sending messages since yesterday, but it has stopped. I've tried adding breakpoints to the code to troubleshoot but it seems that the error only initiates after the method where it saves.

The class that saves the method:

-(IBAction)saveCustomMessage{
    NSString *title = _titleTextField.text;
    NSString *note = _noteTextField.text;
    NSString *explanation = _explainedTextField.text;
    NSString *category = _categoryTextfield.text;
    PFUser *toUser=nil;
    if (public == NO) {
        if ([self.usersTable indexPathForSelectedRow]){
             toUser=[users objectAtIndex:[self.usersTable indexPathForSelectedRow].row];
        }else{
            NSLog(@"no user selected");
            return;
        }



    PFObject *parseMessage = [PFObject objectWithClassName:@"CustomMessage"];
    ParseExampleAppDelegate *delegate=[[UIApplication sharedApplication] delegate];
    parseMessage[@"user"]=delegate.applicationUser;
    parseMessage[@"picture"]=imageData;

    UIImage *image = [_imageView image];
    NSData *imageData = UIImagePNGRepresentation(image);
    PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];


    if (toUser!= nil){
        parseMessage[@"toUser"]=toUser;
    }
        [parseMessage save];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Sent"
                                                    message:@"Your message has been sent."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

    _titleTextField.text=@"";
    _explainedTextField.text=@"";
    _noteTextField.text=@"";
    _categoryTextfield.text=@"";



}

The Error Message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105332f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104073bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000105332e6d +[NSException raise:format:] + 205
    3   My App                           0x0000000101490e08 -[PFObject setObject:forKey:] + 94
    4   My App                         0x0000000101490f8a -[PFObject setObject:forKeyedSubscript:] + 50
    5   My App                         0x0000000101480933 -[CustomMessageViewController saveCustomMessage] + 1235
    6   UIKit                               0x000000010179d8be -[UIApplication sendAction:to:from:forEvent:] + 75
    7   UIKit                               0x00000001018a4410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    8   UIKit                               0x00000001018a37df -[UIControl touchesEnded:withEvent:] + 522
    9   UIKit                               0x00000001017e3308 -[UIWindow _sendTouchesForEvent:] + 735
    10  UIKit                               0x00000001017e3c33 -[UIWindow sendEvent:] + 683
    11  UIKit                               0x00000001017b09b1 -[UIApplication sendEvent:] + 246
    12  UIKit                               0x00000001017bda7d _UIApplicationHandleEventFromQueueEvent + 17370
    13  UIKit                               0x0000000101799103 _UIApplicationHandleEventQueue + 1961
    14  CoreFoundation                      0x0000000105268551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x000000010525e41d __CFRunLoopDoSources0 + 269
    16  CoreFoundation                      0x000000010525da54 __CFRunLoopRun + 868
    17  CoreFoundation                      0x000000010525d486 CFRunLoopRunSpecific + 470
    18  GraphicsServices                    0x00000001051d09f0 GSEventRunModal + 161
    19  UIKit                               0x000000010179c420 UIApplicationMain + 1282
    20  My App
                        0x000000010147c473 main + 115
    21  libdyld.dylib                       0x00000001088f0145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Thanks for any help!

Regards,

Arman

1 Answer

One of the values you area assigning to the PFObject is nil.

print the values you are assigning the the parse object to the console and see which one is nil:

    PFObject *parseMessage = [PFObject objectWithClassName:@"CustomMessage"];
    ParseExampleAppDelegate *delegate=[[UIApplication sharedApplication] delegate];

    //one of these is nil
    NSLog(@"user: %@", delegate.applicationUser);
    NSLog(@"picture: %@", imageData);
    NSLog(@"toUser: %@", toUser);
    parseMessage[@"user"]=delegate.applicationUser;
    parseMessage[@"picture"]=imageData;

    UIImage *image = [_imageView image];
    NSData *imageData = UIImagePNGRepresentation(image);
    PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];


    if (toUser!= nil){
        parseMessage[@"toUser"]=toUser;
    }

Hi Stone Preston

I did what you said, it turns out it was a problem with something I was working on, the images.

Thanks,

Arman