This question already has an answer here:
这个问题在这里已有答案:
- What is a NullReferenceException, and how do I fix it? 33 answers
什么是NullReferenceException,我该如何解决? 33个答案
I have a problem with the development of an application. I am using the eBay API to get information about the items a seller has listed. The Call I use is named "GetSellerList".
我在开发应用程序时遇到问题。我正在使用eBay API获取有关卖家列出的商品的信息。我使用的调用名为“GetSellerList”。
The response might contain a value called "CategoryID" (FreeAddedCategory.CategoryID) if there is a FreeAddedCategory available for the item. You would find a XML like this as response:
如果有可用于项目的FreeAddedCategory,则响应可能包含名为“CategoryID”(FreeAddedCategory.CategoryID)的值。你会发现像这样的XML作为响应:
<ItemArray>
<Item>
<FreeAddedCategory>
<CategoryID>XXXXXXXXX</CategoryID>
</FreeAddedCategory>
</Item>
</ItemArray>
Because this value is not always returned I am getting the following error message:
因为并不总是返回此值,我收到以下错误消息:
Object reference not set to an instance of an object
你调用的对象是空的
I now want to check if this object is available before using it to prevent getting the error. This is how I startet to solve the issue (but without success):
我现在想在使用它之前检查这个对象是否可用,以防止出现错误。这就是我解决问题的方法(但没有成功):
foreach (ItemType item in apiCall.ApiResponse.ItemArray)
{
if (String.IsNullOrWhiteSpace(item.FreeAddedCategory.CategoryID))
{
Console.WriteLine("FreeAdded: Nicht vorhanden!");
}
}
But also this check brings up the same error message. What can I do to make the code working? The logic should be like that:
但是这个检查也会显示相同的错误消息。我该怎么做才能使代码工作?逻辑应该是这样的:
IF item.FreeAddedCategory.CategoryID exist
{
do everything that needs to be done
}
ELSE skip;
1 个解决方案
#1
0
Try:
XElement root = XElement.Load(file); // .Parse(string);
if (root.Descendants("FreeAddedCategory").Any())
{
// do exists
}
else
{
// doesn't exist
}
Or use CategoryId
if that makes more sense for your case.
或者使用CategoryId,如果这对你的情况更有意义。
#1
0
Try:
XElement root = XElement.Load(file); // .Parse(string);
if (root.Descendants("FreeAddedCategory").Any())
{
// do exists
}
else
{
// doesn't exist
}
Or use CategoryId
if that makes more sense for your case.
或者使用CategoryId,如果这对你的情况更有意义。