簡體   English   中英

如何修復'AttributeError:'NoneType'對象在python中沒有屬性'get'錯誤

[英]How to fix 'AttributeError: 'NoneType' object has no attribute 'get' error in python

我已嘗試使用帶有兩個網絡攝像頭的視頻饋送來計算移動物體的距離的代碼。 我有這個錯誤

'AttributeError: 'NoneType' 對象沒有屬性 'get'

當我運行代碼時。 我該如何解決?

def next(self,black=True,wait=0):
    # black frame default
    if black:
        frame = self.black_frame

    # no frame default
    else:
        frame = None

    # get from buffer (fail if empty)
    try:
        frame = self.buffer.get(timeout=wait)
        self.frames_returned += 1
    except queue.Empty:
        print('Queue Empty!')
        #print(traceback.format_exc())
        pass

    # done
    return frame

你得到的錯誤說明了一切,

     frame = self.buffer.get(timeout=wait)

緩沖區為無,要么您沒有在構造函數中定義它,要么在此函數之前的某個地方將其設置為None

你可以添加一個 if else 塊:

if self.buffer: # This will test if the buffer is not None
    # Do access the get method. 
else: 
    # Say buffer is None or do something else. 

更明確:

if self.buffer is not None:
    # Do access the get method. 
else: 
    # Say buffer is None or do something else. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM