处理元素列表时处理NullReferenceException(Handle NullReferenceException when processing a list of elements)
在当前场景中,我正在填充列表但是当我转到
ItemsinList
,许多值将为null
,并且您可以看到我试图控制字符串null值但它给了我错误。 当我在代码的列表行上填充数据源时,它会将Object reference not set to an instance of an object
的错误Object reference not set to an instance of an object
。 我已经尝试过IsNullOrEmpty
,Equals(string.Empty)
并将其默认值设为null
但它仍然会中断。 我的问题是如何处理空值?var items = CommonList.ItemList(); items.Insert(0,new DataModel.SKU(){SKUID = 0,SKUName = "abc" }); var itemsinlist = from obj in items select new { obj.SKUID, obj.SKUName,string.IsNullOrEmpty(obj.SKUCode), obj.SalePrice, obj.PurchasePrice, obj.CostPrice, // if is null the code breaks obj.BaseUnit.UnitName, obj.SKUGroup.SKUGroupName, obj.SKUBrand.SKUBrandName, obj.Unit_Weight }; var objlist = itemsinlist as IEnumerable<dynamic>; // can not add list if some value is null althought the db value can be null this.cmbSKU.ValueMember = "SKUID"; this.cmbSKU.DisplayMember = "SKUName"; this.cmbSKU.DataSource = itemsinlist.ToList();
In the current scenario I’m filling the list but when I go to the
ItemsinList
, many of the value will benull
and as you can see I tried to control the string null value but it gives me the error. As I fill the data source on the list line of the code it gives the error ofObject reference not set to an instance of an object
. I’d already triedIsNullOrEmpty
,Equals(string.Empty)
and given it default value asnull
but its still breaks. My question is how can I handle the null values?var items = CommonList.ItemList(); items.Insert(0,new DataModel.SKU(){SKUID = 0,SKUName = "abc" }); var itemsinlist = from obj in items select new { obj.SKUID, obj.SKUName,string.IsNullOrEmpty(obj.SKUCode), obj.SalePrice, obj.PurchasePrice, obj.CostPrice, // if is null the code breaks obj.BaseUnit.UnitName, obj.SKUGroup.SKUGroupName, obj.SKUBrand.SKUBrandName, obj.Unit_Weight }; var objlist = itemsinlist as IEnumerable<dynamic>; // can not add list if some value is null althought the db value can be null this.cmbSKU.ValueMember = "SKUID"; this.cmbSKU.DisplayMember = "SKUName"; this.cmbSKU.DataSource = itemsinlist.ToList();
原文:https://stackoverflow.com/questions/34219308
最满意答案
相关问答
更多-
ul { list-style-type: none; margin: 0; padding: 0; } ul li ul, ul ul li { display: inline-block; } 见 JsFiddle ul { list-style-type: none; margin: 0; padding: 0; } ul li ul, ul ul li { display: inline-block; } See JsFiddle
-
响应无序列表(Responsive unordered List)[2022-03-14]
媒体查询将为您完成,请根据需要调整并调整以满足您的需求: https : //jsfiddle.net/16q2xr8k/28/ @media (max-width: 640px){ .product-features { display: inline; } } Media queries will do it for you, see fiddle and adjust it as needed to fit what you need: https://jsfiddle.net/1 ... -
段落旁边的无序列表,在同一级别上,没有重叠(Unordered list beside paragraph, on same level, without overlapping)[2023-10-26]
你不应该只是浮动li 。 你必须内联你的p和浮动nav到右边http://jsfiddle.net/Zz6Xs/12/ nav { float: right; } .subtitle { display: inline; ... nav ul { margin-top: 0; } 默认填充和边距设置适用于大多数HTML元素,它们因浏览器而异。 许多人使用CSS重置系统来获得跨浏览器的一致外观。 请参阅此示例,就像您拥有的那样,但必须自己重置边距 如果你可以使用H ... -
不推动第一级导航的关键是放置位置:绝对; 左:0; 在二级导航。 你必须添加位置:相对; 到li.sub-li The key to not pushing the 1st level nav over is to put position: absolute; left: 0; on the 2nd level nav. You'll have to add position: relative; to li.sub-li
-
试图在flexbox的同一行上显示两个无序列表(Trying to display two unordered lists on the same line with flexbox)[2022-04-23]
看起来只是声明导航作为一个弹性元素的伎俩,没有任何进一步的灵活设置。 值得注意的是,它的行为方式是行方向,因为行方向是默认方向。 关于flex-wrap ,这里根本就没有必要,因为没有太多的内容可以包装(或者不会,这取决于你的目标)。 这是一张来自mozilla的精彩照片 ,展示了flex-wrap的使用。 justify-content是需要为整个flex组设置的属性。 这是展示使用的图片 。 我相信您会希望左侧导航条贴在左侧,右侧导航栏贴在右侧。 在这种情况下,您只需指定justify-content: ... -
您可以使用jquery检查在收听事件时是否按下了修改键。 您可以检查传递给处理程序的事件对象上的以下键: altKey - alt/option key ctrlKey - control key shiftKey - shift key metaKey - control key on PCs, control and/or command key on Macs 例: var lis = $('.list li').click(function(e){ if(!e.ctrlKey) { ...
-
不知道你为什么要包含那么多的js代码进行排序。 您需要做的就是为列表分配一些id / class并将其标记为可排序函数。 请在下面查看修改后的代码: 修改后的代码 $(function(){ $(“#sortable”)。sortable(); $(“#sortable”)。disableable(); }); 检查以下js小提琴链接的实例: jsfiddle.net/mxMKs/ Not sure why you have included that much js code for sorting. A ...
-
优化两级SQL查询(Optimizing a two-level SQL query)[2022-05-17]
你的方法很好,虽然它似乎可能会返回重复。 您可能会看到其中一个更快: SELECT C.* FROM C WHERE EXISTS (SELECT 1 FROM B1 WHERE B1.id = C.id_B1 AND B1.id_A = 123) OR EXISTS (SELECT 1 FROM B2 WHERE B2.id = C.id_B2 AND B2.id_A = 123); 这对索引最有效。 “B”表中id的索引很好,但(id, id_A)会更好。 要么: SELECT DIST ... -
import urllib, bs4 pages = bs4.BeautifulSoup(urllib.urlopen(your_url).read()) lists = pages('ul') # your list of unordered list elements import urllib, bs4 pages = bs4.BeautifulSoup(urllib.urlopen(your_url).read()) lists = pages('ul') # your list of uno ...
-
您可以使用flexbox对齐项目,然后使用媒体查询更改较小屏幕的显示属性(将320px更改为断点的任何宽度)。 .flexcontainer { display: flex; align-items: center; } @media (max-width: 320px) { .flexcontainer { display: block; } }
- It ...
相关文章
更多- Storm - Guaranteeing message processing
- Apache Samza - Reliable Stream Processing atop Apache Kafka and Hadoop YARN
- Becoming a data scientist
- [转]So You Want To Be A Producer
- Python 列表(list)操作
- FreeMarker集合(List、Map)
- A Great List of Windows Tools
- How to Start a Business in 10 Days
- Drupal Forums instead of phpBB or vBulletin: A casestudy
- Python内建函数(A)
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)