Python " AttributeError: 'NoneType' object has no attribute " suddenly appearing

Python " AttributeError: 'NoneType' object has no attribute " suddenly appearing

所以我在代码开始时创建了多个 5 'Player' 对象,每个 'Player' 对象大约有 24 个不同的 'Skill' 对象,我用它来循环我的代码

first_run = True

while True:
    for i in range(len(player_name)):
    if first_run is True:
        player[i] = Player(player_name[i], player_url[i], player_thumbnail[i])
        print(player[i].name, "'s Object Created")

    data = requests.get(player_url[i])
    statusCheck(data.status_code, player_name[i])
    data_processed = re.split(', |\n', data.text)

    storeData(data_processed, player[i], first_run)

logging.info("Sleeping for 10 Seconds")
time.sleep(10)
first_run = False

基本上在这个 while 循环的第一个 运行 中,它将创建 5 个具有预定义名称的播放器对象,这些名称存储在上面的列表中。现在,当我的代码达到 storeData(data_processed, player[i], first_run) 时,它在该函数中遇到了一个问题,一个问题是在创建第一个播放器对象时创建的属性不存在,它工作得非常完美,然后突然停止工作,我没有甚至改变任何东西,突然它不再工作,这里是函数“storeData”(下面是日志错误)

def storeData(data_processed, player, first_run):
line_count = 0
for line in data_processed:
    if line != '':
        skill_data = line.split(',')
        if line_count < 24:
            player_skill = None
            player_skill = getattr(player, skills[line_count])
            player_skill.rank = skill_data[0]
            player_skill.level = skill_data[1]
            player_skill.xp = skill_data[2]
            line_count = line_count + 1
            if player_skill.last_level != player_skill.level and first_run is not True:
                logging.info(player.name, "Gained A Level in ", skills[line_count])
                pushLevelUp(discordUrl,skills[line_count-1], player, player_skill)
            player_skill.last_level = skill_data[1]

        elif line_count < 79:
            player_activity = getattr(player, activity[line_count - 24])
            player_activity.last_count = player_activity.count
            player_activity.rank = skill_data[0]
            player_activity.count = skill_data[1]
            line_count = line_count + 1

下面是日志错误,我没有像我说的那样改变任何东西,但它不再找到 player.skill.Overall(使用 getattr)

    Traceback (most recent call last):
  File "*hidden*", line 42, in <module>
    storeData(data_processed, player[i], first_run)
  File "*hidden*", line 142, in storeData
    player_skill = getattr(player, skills[line_count])
AttributeError: 'NoneType' object has no attribute 'Overall'

Process finished with exit code 1

这些是我存储“技能”数据的列表

skills = ['Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking',
      'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore',
      'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecrafting', 'Hunter', 'Construction']

这里是快速运行下

player_skill = getattr(player, skills[line_count]) 第一次迭代应该直接指向传递给 storeData 函数的播放器对象,对于这个例子,我使用“播放器”作为对象名称...首先是 getattr迭代应该直接指向 player.Overall,这将允许我调用 player.Overall.level,但是它说在使用 player_skill = getattr(player, skills[line_count])

时找不到 player.Overall

这是我的 .py 文件 https://pastebin.com/q2VEFGfk ( this is file called 'runebotClassData' ) and this https://pastebin.com/h7BEckgR 是 ( runebotTest.py )

就像我说的,我什么都没改变,api 似乎完全正常地返回数据,并且发生了错误,请帮助

哇我完全忽略了这一点,我被错误指向播放器 [i] 被调用的位置而不是它定义的位置蒙蔽了双眼,如果我的测试模式打开它就没有定义(first_run = False ) 我应该多注意声明