Python TypeError: 'NoneType' 对象没有属性 '__getitem__'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13907949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:55:17  来源:igfitidea点击:

TypeError: 'NoneType' object has no attribute '__getitem__'

pythontypeerror

提问by user1908896

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:

我遇到了一个问题,我不知道为什么会发生这种情况以及如何解决它。我正在使用 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__'

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?

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

回答by user1908896

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.valuesis None.

布伦巴恩是对的。该错误意味着您尝试执行类似None[5]. 在回溯中,它说self.imageDef=self.values[2],这意味着您self.valuesNone

You should go through all the functions that update self.valuesand make sure you account for all the corner cases.

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

回答by mlcr

The function move.CompleteMove(events)that you use within your class probably doesn't contain a returnstatement. So nothing is returned to self.values(==> None). Use returnin move.CompleteMove(events)to return whatever you want to store in self.valuesand it should work. Hope this helps.

move.CompleteMove(events)您在类中使用的函数可能不包含return语句。所以没有返回到self.values(==> None)。使用returninmove.CompleteMove(events)返回您想要存储的任何内容self.values,它应该可以工作。希望这可以帮助。

回答by Burhan Khalid

move.CompleteMove()does not return a value (perhaps it just prints something). Any method that does not return a value returns None, and you have assigned Noneto self.values.

move.CompleteMove()不返回值(也许它只是打印一些东西)。任何不返回值的方法都会返回None,并且您已分配Noneself.values.

Here is an example of this:

这是一个例子:

>>> def hello(x):
...    print x*2
...
>>> hello('world')
worldworld
>>> y = hello('world')
worldworld
>>> y
>>>

You'll note ydoesn't print anything, because its None(the only value that doesn't print anything on the interactive prompt).

您会注意到y不打印任何内容,因为它None(在交互式提示上不打印任何内容的唯一值)。