python - TypeError: 'NoneType' 对象没有属性 '__getitem__'

标签 python typeerror

我遇到了问题,我不知道为什么会发生这种情况以及如何解决它。我正在使用 python 和 pygame 开发视频游戏,但出现此错误:

 File "/home/matt/Smoking-Games/sg-project00/project00/GameModel.py", line 15, in Update 
   self.imageDef=self.values[2]
TypeError: 'NoneType' object has no attribute '__getitem__'

代码:

import pygame,components
from pygame.locals import *

class Player(components.Entity):

    def __init__(self,images):
        components.Entity.__init__(self,images)
        self.values=[]

    def Update(self,events,background):
        move=components.MoveFunctions()
        self.values=move.CompleteMove(events)
        self.imageDef=self.values[2]
        self.isMoving=self.values[3]

    def Animation(self,time):
        if(self.isMoving and time==1):
            self.pos+=1
            if (self.pos>(len(self.anim[self.imageDef])-1)):
                self.pos=0
        self.image=self.anim[self.imageDef][self.pos]

你能解释一下这个错误是什么意思以及为什么会发生这样我就可以修复它吗?

最佳答案

BrenBarn 是正确的。该错误表示您尝试执行 None[5] 之类的操作。在回溯中,它说 self.imageDef=self.values[2],这意味着您的 self.valuesNone

您应该检查所有更新 self.values 的函数,并确保考虑到所有极端情况。

关于python - TypeError: 'NoneType' 对象没有属性 '__getitem__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13907949/

相关文章:

python - 如何将本地 API 与在不同端口提供服务的 Vue 应用程序一起使用?

python - Gmail API : "Invalid Label" When Trying to Set Message Label to One That I Created

python-3.x - 类型错误:切片索引必须是整数或无或具有 __index__ 方法

python - 无法将值插入 QSqlDatabase

javascript - 未捕获的类型错误 : Cannot call method 'innerHTML' of null even when I place my script code inside <body> tag

python - Pandas 中的数据透视表 - 不可排序的类型

python - 幻像类型错误(Python 3.3)

python - Django 模型线程安全吗?

python - QTableWidget强制单项选择

python - Qtimer 不工作