TypeError: 'NoneType' 对象没有属性 '__getitem__' [英] TypeError: 'NoneType' object has no attribute '__getitem__'
问题描述
我遇到了一个问题,我不知道为什么会发生这种情况以及如何解决它.我正在使用 python 和 pygame 开发视频游戏,但出现此错误:
文件/home/matt/Smoking-Games/sg-project00/project00/GameModel.py",第 15 行,更新self.imageDef=self.values[2]TypeError: 'NoneType' 对象没有属性 '__getitem__'
代码:
import pygame,components从 pygame.locals 导入 *类播放器(组件.实体):def __init__(self,images):components.Entity.__init__(self,images)self.values=[]def 更新(自我,事件,背景):move=components.MoveFunctions()self.values=move.CompleteMove(事件)self.imageDef=self.values[2]self.isMoving=self.values[3]def动画(自我,时间):if(self.isMoving and time==1):self.pos+=1如果 (self.pos>(len(self.anim[self.imageDef])-1)):self.pos=0self.image=self.anim[self.imageDef][self.pos]
你能向我解释这个错误是什么意思以及它为什么会发生以便我可以修复它吗?
BrenBarn 是正确的.该错误意味着您尝试执行诸如 None[5]
之类的操作.在回溯中,它说 self.imageDef=self.values[2]
,这意味着您的 self.values
是 None
.>
您应该检查更新 self.values
的所有函数,并确保考虑到所有极端情况.
I'm having an issue and I have no idea why this is happening and how to fix it. I'm working on developing a Videogame with python and pygame and I'm getting this error:
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__'
The code:
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]
Can you explain to me what that error means and why it is happening so I can fix it?
BrenBarn is correct. The error means you tried to do something like None[5]
. In the backtrace, it says self.imageDef=self.values[2]
, which means that your self.values
is None
.
You should go through all the functions that update self.values
and make sure you account for all the corner cases.
这篇关于TypeError: 'NoneType' 对象没有属性 '__getitem__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!