Call to a member function label() on a non-object in entity module I created an entity type using Entity API and made it exportable. The export link works properly but on Import I have this error:


  Fatal error: Call to a member function label() on a non-object in [...]\sites\all\modules\entity\entity.module on line 903


I couldn't find any reason.
Here's my hook_entity_info():

$return = array(
  'event' =&gt; array(
    'label' =&gt; t('Event'),
    'entity class' =&gt; 'EventClass',
    'controller class' =&gt; 'EventController',
    'base table' =&gt; 'calendar_events',
    'fieldable' =&gt; TRUE,
    'entity keys' =&gt; array(
      'id' =&gt; 'eid',
    ),
    'load hook' =&gt; 'events_load',
    'view modes' =&gt; array(
      'full' =&gt; array(
        'label' =&gt; t('Default'),
        'custom settings' =&gt; FALSE,
      ),
    ),
    'label callback' =&gt; 'entity_class_label',
    'uri callback' =&gt; 'entity_class_uri',
    'module' =&gt; 'events',
    'access callback' =&gt; 'event_access',
    'exportable' =&gt; TRUE,
    'admin ui' =&gt; array(
      'path' =&gt; 'admin/structure/events',
      'file' =&gt; 'events.admin.inc',
      'controller class' =&gt; 'EventsUIController',
    ),
  ),
);


Tell me if you need more information.

[EDIT]
I figured that the error has been occurred in entity_class_label($entity) in sites/all/modules/entity/entity.module:903. I printed the content of $entity and it seems it is NULL!
Why is it NULL?
It looks like this is a bug in the Entity API module. 

An issue with exactly the same error message was reported here to do with the Profile2 module (which uses Entity API pretty heavily).

The upshot of that post is that the problem is an Entity API bug, details of which you can see at: Load reference field when building contents.

As you can see from the above there are patches available to fix the problem. The most recent patch that passed testing was on the 16th November 2011.

When you look at the Entity API module's latest stable release you can see it's date sas the 10th November 2011. This means that the patch probably hasn't made it into the stable version yet, so won't have been fixed in your version of the module.

There are two things you can do:


Upgrade to the dev version of the Entity API module (of course the usual amount of caution should be taken and you probably won't want to do this for a live site).
Apply the patch to the entity.controller.inc file that seems to have solved the problem for other users.