2015-05-31 4 views
-1

I, как питон новичок, получить следующее сообщение об ошибке при выполнении кода, который я получил от автора:AttributeError: объект NoneType не имеет атрибута

line 412, in add_family 
    mother_birth_ref = mother.get_birth_ref() 
AttributeError: 'NoneType' object has no attribute 'get_birth_ref' 

Соответствующая часть кода должна быть эта один здесь:

def write_child_stats(self): 
    file = open("%s/intern/child-stats.txt" % self.dir_name, 'w') 

def add_family(family, marriage_event, divorce_event): 
    father_handle = family.get_father_handle() 
    mother_handle = family.get_mother_handle() 

    father = self.database.get_person_from_handle(father_handle) 
    father_birth_ref = father.get_birth_ref() 
    father_birth = None 
    if father_birth_ref: 
     father_birth = self.database.get_event_from_handle(father_birth_ref.ref) 

    mother = self.database.get_person_from_handle(mother_handle) 
    mother_birth_ref = mother.get_birth_ref() 
    mother_birth = None 
    if mother_birth_ref: 
     mother_birth = self.database.get_event_from_handle(mother_birth_ref.ref) 

    children = [] 
    child_refs = family.get_child_ref_list() 
    for child_handle in child_refs: 
     child = self.database.get_person_from_handle(child_handle.ref) 
     if child: 
      child_birth_ref = child.get_birth_ref() 
      if child_birth_ref: 
       child_birth = self.database.get_event_from_handle(child_birth_ref.ref) 
       children.append("%04d-%02d-%02d" % child_birth.get_date_object().get_ymd()) 

Как исправить проблему?

+0

'get_person_from_handle' возвращает' 'None' для mother_handle'. Отправьте его. – Maroun

ответ

1
mother = self.database.get_person_from_handle(mother_handle) 

Эта линия возвращает None. Проверьте get_person_from_handle функция.

В качестве альтернативы, вы можете добавить проверку:

mother = self.database.get_person_from_handle(mother_handle) 
mother_birth_ref = None 
mother_birth = None 
if mother: 
    mother_birth_ref = mother.get_birth_ref() 
if mother_birth_ref: 
    mother_birth = self.database.get_event_from_handle(mother_birth_ref.ref) 
+0

Спасибо, Рахул Гупта, это решает мою проблему. –

Смежные вопросы