2013-07-09 61 views
1

我不斷收到這個錯誤,我有點卡住了。我知道這與代碼的最後一點有關,但除此之外,我大多都感到困惑,我不確定如何去解決它/實現修復。AttributeError:'NoneType'對象沒有屬性'userprofile'

The section that calls Panel (part of a visual options menu):

self.AutojoinMemosPanel = GUI.Options.Panels.AutojoinMemos.Panel(self.config,self) 
    self.pages.addWidget(self.AutojoinMemosPanel) 

Panel:

from PyQt4 import QtCore 
from PyQt4 import QtGui 

class MemoEntryItem(QtGui.QListWidgetItem): 
def __init__(self,MemoName): 
    QtGui.QListWidgetItem.__init__(self,MemoName) 
    self.MemoName = MemoName 

class Panel(QtGui.QWidget): 
def __init__(self,Config,userprofile): 
    QtGui.QWidget.__init__(self) 
    self.Config = Config 

    self.AutoIdentifyCheck = QtGui.QCheckBox("Automatically identify with nickserv") 
    self.PasswordLabel = QtGui.QLabel("Password:") 
    self.PasswordEntry = QtGui.QLineEdit() 
    self.PasswordEntry.setEchoMode(QtGui.QLineEdit.Password) 

    self.TitleLabel = QtGui.QLabel("Memos to join on startup:") 
    self.MemosList = QtGui.QListWidget() 
    self.MemoNameEntry = QtGui.QLineEdit() 
    self.RemoveButton = QtGui.QPushButton("REMOVE") 
    self.AddButton = QtGui.QPushButton("ADD") 

    Layout = QtGui.QVBoxLayout(self) 
    Layout.setAlignment(QtCore.Qt.AlignTop) 
    Layout.addWidget(self.AutoIdentifyCheck) 
    NickservPasswordLayout = QtGui.QHBoxLayout() 
    NickservPasswordLayout.addWidget(self.PasswordLabel) 
    NickservPasswordLayout.addWidget(self.PasswordEntry) 
    Layout.addLayout(NickservPasswordLayout) 
    Layout.addWidget(self.TitleLabel) 
    Layout.addWidget(self.MemosList) 
    Layout.addWidget(self.MemoNameEntry) 
    AddRemoveButtonLayout = QtGui.QHBoxLayout() 
    AddRemoveButtonLayout.addWidget(self.RemoveButton) 
    AddRemoveButtonLayout.addWidget(self.AddButton) 
    Layout.addLayout(AddRemoveButtonLayout) 

    self.connect(self.RemoveButton,QtCore.SIGNAL("clicked()"),self,QtCore.SLOT("RemoveSelectedMemo()")) 
    self.connect(self.AddButton,QtCore.SIGNAL("clicked()"),self,QtCore.SLOT("AddMemoFromTextEntry()")) 

    if self.Config.userprofile.userprofile.get("AutoIdentify",False): 
     self.AutoIdentifyCheck.setChecked(True) 

    self.PasswordEntry.setText(self.Config.userprofile.userprofile.get("NickservPassword","")) 

    for MemoName in self.Config.config.get("AutojoinMemos",[]): 
     self.AddMemoEntry(MemoName) 

@QtCore.pyqtSlot() 
def RemoveSelectedMemo(self): 
    self.MemosList.takeItem(self.MemosList.currentRow()) 

@QtCore.pyqtSlot() 
def AddMemoFromTextEntry(self): 
    MemoName = str(self.MemoNameEntry.text()).strip() 
    if (MemoName != ""): 
     self.AddMemoEntry(MemoName) 
     self.MemoNameEntry.setText("") 

def AddMemoEntry(self,MemoName): 
    self.MemosList.addItem(MemoEntryItem(MemoName)) 

def SaveOptions(self): 
    MemosListed = [] 

    for MemoIndex in range(self.MemosList.count()): 
     MemoItem = self.MemosList.item(MemoIndex) 
     MemosListed.append(MemoItem.MemoName) 

    self.Config.set("AutojoinMemos",MemosListed) 

    self.Config.userprofile.userprofile["AutoIdentify"] = self.AutoIdentifyCheck.isChecked() 
    self.Config.userprofile.userprofile["NickservPassword"] = str(self.PasswordEntry.text()) 
    self.Config.userprofile.save() 

任何幫助嗎?

+0

可能想要將語言標記添加到問題中(例如[python])。 –

+1

看起來像'self.Config.userprofile'是'None' – karthikr

+3

@karthikr或'self.Config'。 – thomasd

回答

3

首先想到,這裏可能會發生兩件事之一。進一步檢查後,只有一個。

假設,因爲你沒有明確說明,有問題的路線是:

if self.Config.userprofile.userprofile.get("AutoIdentify",False): 

然後,當你的錯誤

AttributeError: 'NoneType' object has no attribute 'userprofile'

意味着你試圖訪問一個不存在的屬性(這是has no attribute 'userprofile'後面的含義),而對象您檢查的屬性不存在,因爲o的類型不存在的對象是NoneType(這是'NoneType' object背後的含義)。

因此,要調試,您希望查找訪問userprofile的對象可能爲None(即未分配或空值)的可能性。這是可能的,在

self.Config.userprofile #1 

self.Config.userprofile.userprofile #2 

在#1中,如果配置的值是None,然後將self.Config.userprofile拋出AttributeError的因爲ConfigNoneType對象,因此具有沒有屬性 - - 單獨一個userprofile屬性。

在#2如果userprofileself.Congig值爲None,然後將self.Config.userprofile.userprofile拋出AttributeError的因爲userprofileNoneType對象,因此無論是沒有屬性。

但是,您可以消除這兩個選項之一。

想想看,#2可能不會拋出這個錯誤,因爲如果對象self.ConfigNone(如上expained)的屬性userprofile,那麼你就先拋出一個不同的錯誤:

AttributeError: 'Config' object has no attribute 'userprofile' 

如果Config對象確實有一個屬性userprofile,那麼你會得到錯誤:

AttributeError: 'userprofile' object has no attribute 'userprofile' 

因此,Config必須是None,這意味着您不會將Config的值傳遞給__init__

希望這有助於解決您的問題並更好地自行調試!

+0

這是一個夢幻般的步行 –

+0

我將如何去傳遞一個值爲__init__的Config? – user2566140

+0

您目前如何設置Config的值? – llb