陷阱null仍会触发空引用异常 [英] Trapping null still fires a null reference exception
本文介绍了陷阱null仍会触发空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个列表来存储项目,但是当一个项目不存在时,我正在捕获null,但是当我得到一个对象时,它仍然会得到一个空引用,它会在该行崩溃
I am using a list to store items but when an item does not exist i am trapping null but when i get an object its still getting a null reference it crashing at the line
CategoryCodeDesc = categorydesc.description;
任何想法如何更好地陷入此
Any ideas how better to trap for this
string CategoryCodeValue, CategoryCodeDesc;
if (categorycode == null)
{
CategoryCodeValue = "";
CategoryCodeDesc = "";
}
else
{
CategoryCodeValue = categorycode.LookupValue.ToString();
CategoryCodeDesc = categorydesc.description;
}
推荐答案
这是一个技巧问题吗?
Is this a trick question?
if (categorycode == null)
{
CategoryCodeValue = "";
}
else
{
CategoryCodeValue = categorycode.LookupValue.ToString();
}
if (categorydesc == null)
{
CategoryCodeDesc = "";
}
else
{
CategoryCodeDesc = categorydesc.description;
}
这篇关于陷阱null仍会触发空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文