Fatal error: Call to a member function raw() on a non-object in I am getting this error 


  Fatal error: Call to a member function raw() on a non-object in one of my custom modules.


Following is my function:

function _gocoop_services_get_app_instance($wrapper)
{
  $instance_id = $wrapper-&gt;field_seller-&gt;field_application_instance-&gt;raw();
  if (!$instance_id)
  {
    $query = new EntityFieldQuery();
    $query-&gt;entityCondition('entity_type', 'application_instance')
        -&gt;entityCondition('bundle', 'application_instance')
        -&gt;fieldCondition('field_internal', 'value', 1);
    $result = $query-&gt;execute();
    if ($result['application_instance'])
    {
      $instance_id = reset(array_keys($result['application_instance']));
    }
  }
  return $instance_id;
}


Have used the above function at couple of places like shown below:

$wrapper = entity_metadata_wrapper('commerce_product', $entity);
$instance_id = _gocoop_services_get_app_instance($wrapper);


Can anyone help me whats wrong as this has occurred all of a sudden.
You could try something like this instead, checking if the field value is set first and catching the exception.

function _gocoop_services_get_app_instance($wrapper) {
  $instance_id = FALSE;
  try {
    // Check if wrapper field set first
    if (isset($wrapper-&gt;field_seller) &amp;&amp; isset($wrapper-&gt;field_seller-&gt;field_application_instance)) {
      $instance_id = $wrapper-&gt;field_seller-&gt;field_application_instance-&gt;raw();
    }
    if (!$instance_id) {
      $query = new EntityFieldQuery();
      $query-&gt;entityCondition('entity_type', 'application_instance')
          -&gt;entityCondition('bundle', 'application_instance')
          -&gt;fieldCondition('field_internal', 'value', 1);
      $result = $query-&gt;execute();
      if ($result['application_instance']) {
        $instance_id = reset(array_keys($result['application_instance']));
      }
    }
  } catch (EntityMetadataWrapperException $e) {
    watchdog_exception('gocoop', $e);
  }
  return $instance_id;
}


You might also want to use a static cache for $instance_id, but that would need to vary based on your $wrapper id value.