2015-07-04 205 views
-2

此代碼引用對象時接收錯誤返回錯誤:未設置爲一個對象在foreach循環

這是對的foreach線拋出的實例

對象引用。

GenericInventory object = new GenericInventory (SteamWeb); 

foreach(var item in object.items) 
{ 
//do stuff    
} 

我不知道爲什麼我得到這個錯誤。我如何解決它?

非常感謝您的幫助,如果這個問題過於簡單,或者已經被問到,我很抱歉。

+2

因爲'items'是'null'。按照「GenericInventory」中的邏輯找出原因。 –

+0

你的'object.items'是否包含列表或數組,或者它是空的? ? – gypsyCoder

+0

在[one]之前已經回答了很多次(https://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-意思是)[two](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Equalsk

回答

0

嘗試將GenericInventory的實例重命名爲object以外的其他值;可能是obj

GenericInventory obj = new GenericInventory (SteamWeb);