I found the cause when it occurs on a contribution page (Like @nic found), and I'm pretty sure the fix is the same for events.
This error occurs when you have "view my contact" and/or "edit my contact" permission on the anonymous user - who, by definition, does not have a contact.
You must also have a profile on the page which contains a contribution or contact field.
I'm submitting a patch, so hopefully this is a non-issue in CiviCRM 5.70+ or so, but you can fix it now by changing your CMS's user permissions.