如何知道数组是否为空。 a[10]比如数组长度是10,但是数组前8个有数据,后面2个没有赋值。 那如何进行判断是否为空呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只能一个一个判断,如果是对象数组就判断是否为null我觉得你应该用List模版类来处理更合适。 用哪个语句 is “” ,is null,.Equals("") 都判断不出 c#好像直接有方法实现toarraylist吧?arraylist里面可以直接判断是否为空。 if( a[10] == null ) MessageBox.Show("空"); for (i=0;i<10;i++){if (a[i]==null){MessageBox.Show(a[i]+"为空!");}} role[5,12];for (int a = 0; a < role.GetLength(0); a++){ for (int b = 0; b < role.GetLength(1); b++) { if (role[a, b].ToString().Trim() !=null)//错误提示:未处理NullReferenceException { LVItem.SubItems.Add(role[a, b].ToString()); } }}}未处理 System.NullReferenceException Message="NullReferenceException" StackTrace: 位于 MobileTax.cls.img.PopulateListView() 位于 MobileTax.ermenu.ermenu_Load() 位于 System.Windows.Forms.Form.OnLoad() 位于 System.Windows.Forms.Form._SetVisibleNotify() 位于 System.Windows.Forms.Control.set_Visible() 位于 System.Windows.Forms.Control.Show() 位于 MobileTax.cls.img.ListView1_ItemActivate() 位于 System.Windows.Forms.ListView.OnItemActivate() 位于 System.Windows.Forms.ListView.HandleItemActivate() 位于 System.Windows.Forms.ListView.WnProc() 位于 System.Windows.Forms.Control._InternalWnProc() 位于 Microsoft.AGL.Forms.EVL.EnterModalDialog() 位于 System.Windows.Forms.Form.ShowDialog() 位于 MobileTax.Form1.timer1_Tick() 位于 System.Windows.Forms.Timer._WnProc() 位于 System.Windows.Forms.ApplicationThreadContext._InternalContextMessages() 位于 Microsoft.AGL.Forms.EVL.EnterMainLoop() 位于 System.Windows.Forms.Application.Run() 位于 MobileTax.Program.Main() 问题解决 方法如下: if (role[a, b] != null) // ToString() 是 Object 的方法,而 null 不是 Object 连接数据库,绑定DataGridView之后的问题 另一项目已打开了该文档 c#不同版本的编程规范和用法有什么不一样? 自学一年多,快毕业了,好烦恼!帮帮我!! 线程增长问题 DataGridView 的用法问题 Hashtable的疑惑 一般做生成注册码程序中都用到什么算法。。 WinForm下 .NET Data Provider for Oracle乱码问题? 初学者问题 如何判断数组某个元素是否为空。 200分求解! 截取某个字符串的问题。
{
if (a[i]==null)
{
MessageBox.Show(a[i]+"为空!");
}
}
for (int a = 0; a < role.GetLength(0); a++)
{
for (int b = 0; b < role.GetLength(1); b++)
{
if (role[a, b].ToString().Trim() !=null)//错误提示:未处理NullReferenceException
{
LVItem.SubItems.Add(role[a, b].ToString());
}
}
}
}未处理 System.NullReferenceException
Message="NullReferenceException"
StackTrace:
位于 MobileTax.cls.img.PopulateListView()
位于 MobileTax.ermenu.ermenu_Load()
位于 System.Windows.Forms.Form.OnLoad()
位于 System.Windows.Forms.Form._SetVisibleNotify()
位于 System.Windows.Forms.Control.set_Visible()
位于 System.Windows.Forms.Control.Show()
位于 MobileTax.cls.img.ListView1_ItemActivate()
位于 System.Windows.Forms.ListView.OnItemActivate()
位于 System.Windows.Forms.ListView.HandleItemActivate()
位于 System.Windows.Forms.ListView.WnProc()
位于 System.Windows.Forms.Control._InternalWnProc()
位于 Microsoft.AGL.Forms.EVL.EnterModalDialog()
位于 System.Windows.Forms.Form.ShowDialog()
位于 MobileTax.Form1.timer1_Tick()
位于 System.Windows.Forms.Timer._WnProc()
位于 System.Windows.Forms.ApplicationThreadContext._InternalContextMessages()
位于 Microsoft.AGL.Forms.EVL.EnterMainLoop()
位于 System.Windows.Forms.Application.Run()
位于 MobileTax.Program.Main()