在foreach循环中的空异常处理 [英] Null Exception handling in foreach loop

查看:560
本文介绍了在foreach循环中的空异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串和空值的列表X。我正在迭代foreach循环将值绑定到文本框。如果我在列表X中获得任何空值,foreach循环将被终止,并获得空异常如何处理它。



我正在检查每个循环中的条件。但是我不知道它是不正确的。

  SPList _listObj = web.Lists [new Guid(listID)]; 
SPListItem item = _listObj.GetItemById(Convert.ToInt32(itemID));
foreach(_listObj.Fields中的SPField字段)
{
if(field.Title!= Null)
{//做一些代码}}


解决方案

请尝试以下代码:



< p $ p> foreach(var x in Lists.Where(x => x.fiels!= null))
{

}


I am having the list X with some string and null value . I am iterating the foreach loop to bind the value to the textbox. If I get any null values in my list X the foreach loop get terminated and getting the null exception how to handle it.

I am checking the condition inside the for each loop. but i tnink it not correct logcally.

SPList _listObj = web.Lists[new Guid(listID)];
            SPListItem item = _listObj.GetItemById(Convert.ToInt32(itemID));
           foreach (SPField field in _listObj.Fields)
            {
                if (field.Title != Null)
                {  //do some code}}

解决方案

Try below code:

foreach(var x in Lists.Where(x => x.fiels != null))
{

}

这篇关于在foreach循环中的空异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆