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

Parse method causing "uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for (null)'

When I run the self-destructing messaging app, I get "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)'" It seems to be related to [PFInternalUtils assertValidClassForQuery:] .

When I comment out the following code from inboxViewController.m, I can launch the app and log in:

PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"error %@ %@", error, [error userInfo]);
        } else {
            self.messages = objects;
            [self.tableView reloadData];
            NSLog(@"Got %d messages", [self.messages count]);
        }
    }];

And then I can uncomment the code and run the app in the simulator/ device properly. It's almost like it crashes because it forgot whether I was logged in or something? It looks like I'm not the only one: https://www.parse.com/questions/ios-most-elegant-way-to-catch-assertvalidclassforquery-problems But this didn't help. Any idea?

3 Answers

looks like the currentUser object id here

[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]

is null. see if there is a reason why that is happening. Are you logged in?

When I comment out the code and run it, I can log in, after which the whole app works. Strangely enough, I added an if statement to check whether the objectid is nil:

- (void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
    // prevents error when running for first time in a while
    if ([[PFUser currentUser] objectId] == nil) {
        NSLog(@"No objectID");
    } else {
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"error %@ %@", error, [error userInfo]);
        } else {
            self.messages = objects;
            [self.tableView reloadData];
            NSLog(@"Got %d messages", [self.messages count]);
        }
    }];
    }
}

And now it works. Can't explain that.

This makes sense.

If you're not logged in then it will skip making this query call. If you are logged in it will make the call.

Thanks for posting.

I think the if() prevents the code to run the rest of the query and therefor avoids the crash.