2013-11-15 45 views
1

的一段代碼看起來是這樣的,其是一個Python腳本蟒AttributeError的:「NoneType」對象沒有屬性「了createSession

我認爲這部分是其中誤差在

def getSessionManagementMBean(sessionName): 
    SessionMBean = findService("SessionManagement", "com.bea.wli.sb.management.configuration.SessionManagementMBean") 
    SessionMBean.createSession(sessionName) 
    return SessionMBean 

並且當爬行我們運行腳本,我們碰到這個錯誤

File "/var/cache/chef/weblogic/managed/config-setup/smtpConfig/import.py", line 190, in getSessionManagementMBean 
AttributeError: 'NoneType' object has no attribute 'createSession 

我在想這個

更換上面的代碼
def getSessionManagementMBean(sessionName): 
    SessionMBean = findService("SessionManagement", "com.bea.wli.sb.management.configuration.SessionManagementMBean") 
    Variable = SessionMBean.createSession(sessionName) 
    print Variable 
    return Variable 

任何解決方案或意見,歡迎請

+5

'findService()'調用返回'None'。你可以測試:'如果SessionMBean不是None:SessionMBean.createSession(sessionName)'。但是你可能需要弄清楚爲什麼findService()調用首先失敗。 –

+0

您提出的更改不會修復該異常,因爲異常是由'SessionMBean'引用'None'引起的,並且該對象沒有'.createSession()'方法。 –

+0

是的,那是真的,我想檢查它被改變後該變量的打印是否會返回一個值,但感謝評論 – ameya

回答

0
def getSessionManagementMBean(sessionName): 
    SessionMBean = findService("SessionManagement", 
        "com.bea.wli.sb.management.configuration.SessionManagementMBean") 
    if SessionMBean: # check SessionMBean is not 'None' 
     SessionMBean.createSession(sessionName) 
     return SessionMBean 
    # if SessionMBean is 'None' or 0 then raise your own exception 
    raise Exception("Something happened with SessionMBean") 
+1

這只是引發了一個不同的例外;它與傳遞原始的AttributeError沒有什麼不同。 – chepner

+0

@chepner沒錯,但我個人覺得這個消息更容易理解,而不是典型的「我在某處不應該做某事。」 –

+0

這樣,如果你創建自己的異常,那麼你就可以抓住它並相應地處理它,而不是試圖捕獲一個'AttributeError'異常。 – jramirez

0

也許你有一個方法(findService),它不返回正確的對象或此方法分配的結果,但它不會返回一個對象。 檢查SessionMBean是否是您的正確對象或者是無。

相關問題