读一空单元格,使对象引用错误 [英] reading a empty cell, gives object reference error

查看:144
本文介绍了读一空单元格,使对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我做
Worksheets.Cells [2,5] .value.ToString()读取细胞;



我得到一个错误系统.NullReferenceException:未将对象引用设置到对象的实例



什么是检查null,然后分配的价值,而无需有一个好办法一个如果语句。


解决方案

 字符串strValue的= Worksheets.Cells [2,5 ] .value的== NULL?的String.Empty:Worksheets.Cells [2,5] .value.ToString(); 

 对象objValue = Worksheets.Cells [2,5] .value的?的String.Empty 


When I read a cell by doing Worksheets.Cells[2,5].value.ToString();

I get a error "System.NullReferenceException: Object reference not set to an instance of an object."

What would be a good way to check for null and then assign the value, without having to have a "if" statement.

解决方案

 string strValue = Worksheets.Cells[2,5].value==null ? string.Empty : Worksheets.Cells[2,5].value.ToString();

or

object objValue = Worksheets.Cells[2,5].value ?? string.Empty

这篇关于读一空单元格,使对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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