Search code examples
pythonattributesgetattr

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


so i have multiple 5 'Player' objects being created upon the start of my code, and about 24 different 'Skill' objects for each 'Player' Object, im using this to loop through my code

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

basically on the first run through of this while loop, it will create 5 player objects that have predefined names that are stored in a list above. now when my code reaches storeData(data_processed, player[i], first_run) it reaches an issue in that function, an issue saying that an attribute created when the first player object is created doesnt exist, it was working absolutely perfectly, and then suddenly it stopped working, i didnt even change anything and suddenly its no longer working, here is the function "storeData" ( further below is the log error )

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

below is log error, i didnt change anything as i said, but it no longer finds player.skill.Overall ( using 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

these are the lists in which i store the data for "skills"

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']

here is a quick rundown

player_skill = getattr(player, skills[line_count]) on first iteration should point directly to the player object passed into the storeData function, for this example il use "player", as the object name... the getattr on first iteration should point directly to player.Overall, which would allow me to call player.Overall.level, however it says it cannot find player.Overall when using player_skill = getattr(player, skills[line_count])

here are my .py files https://pastebin.com/q2VEFGfk ( this is file called 'runebotClassData' ) and this https://pastebin.com/h7BEckgR is ( runebotTest.py )

as i said, i changed nothing, the api seems to be returning the data exactly as normal, and that error is occuring, please help


Solution

  • wow i overlooked that completely, i was blinded by the error pointing to where player[i] was being called, not where it was defined, and it wasnt defined if my testing mode was on ( first_run = False ) i should have paid more attention to the declaration