Error in drupal_form_submit when taxonomy reference (tid) is set While trying to creating a node programmatically using 'drupal_form_submit' i've come across a php error in the form.inc code

Here's my code

function vl_sample_data_generate_yvbct1() {

  module_load_include('inc', 'node', 'node.pages');

  global $user;

  $content = 'Generating YVB CT1';
  $form_state = array();
  $data = array();
  $node = (object) array(
    'uid' =&gt; $user-&gt;uid,
    'name' =&gt; $user-&gt;name,
    'type' =&gt; 'yvb_ct1',
    'language' =&gt; LANGUAGE_NONE,
  );
  node_object_prepare($node);

  $data['uid'] = $user-&gt;uid;
  $data['created'] = time();
  $data['type'] = 'yvb_ct1';
  $data['language'] = LANGUAGE_NONE;
  $data['title'] = 'Node created on ' . time();
  $data['status'] = 1;
  $data['op'] = 'Save';

  $data['field_middle_name'][LANGUAGE_NONE][0]['value'] = 'Alex';
  $data['field_gender'][LANGUAGE_NONE][0]['tid'] = 24;

  $form_state['values'] = $data;

  drupal_form_submit('yvb_ct1_node_form', $form_state, $node);

  return $content;
}


The error i get when i run the function (as a menu callback) is 


Warning: Illegal offset type in isset or empty in _form_validate() (line 1333 of /opt/httpd/htdocs/www/staging/jns/trunk/includes/form.inc).
An illegal choice has been detected. Please contact the site administrator.

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of /opt/httpd/htdocs/www/staging/jns/trunk/includes/entity.inc).

Warning: Illegal offset type in taxonomy_field_validate() (line 1519 of /opt/httpd/htdocs/www/staging/jns/trunk/modules/taxonomy/taxonomy.module).

Notice: Trying to get property of non-object in taxonomy_field_validate() (line 1519 of /opt/httpd/htdocs/www/staging/jns/trunk/modules/taxonomy/taxonomy.module).


However, if i comment out the line

  $data['field_gender'][LANGUAGE_NONE][0]['tid'] = 24;


It works fine. Am using Drupal 7.26.
Also, the term tid does exist and belongs to the valid Vocabulary which is 
assigned in the content type.

Any help regarding the above problem is appreciated.

Thanks

Yashesh
It is possible that 24 is not a valid taxonomy term ID in the system, but I'm not sure if that's it. So I would check that using taxonomy_get_term_by_name or something. Also, and perhaps first, make sure to verify the field_gender field exists, as it might not always be there in the system and you don't want nasty bugs down the road.

// Check if field_gender field exists for node type yvb_ct1
$field_instances = field_info_instance('node', 'field_gender', 'yvb_ct1');
if (isset($field_instances['field_gender'])) :

  // Now check if the taxonomy ID is correct
  // I assume you are looking for a term name of 'female'
  if (!empty(taxonomy_get_term_by_name('female', 'gender')))
    // now try setting term reference field value

endif;


This is untested but I hope it might help get you closer.