如何判断数组某个元素是否为空。
role[5,12];//数组role 
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()

解决方案 »

  1.   

    试试看,L@_@K!
    role[5,12];//数组role 
    for (int a = 0; a < role.GetLength(0); a++)
    {
      for (int b = 0; b < role.GetLength(1); b++)
        {                    
            if (role[a, b] != null) // ToString() 是 Object 的方法,而 null 不是 Object
                 {
                    LVItem.SubItems.Add(role[a, b].ToString());
                  }
         }
    }
      

  2.   

    if (role[a,b] != null && !role[a, b].ToString().Trim().IsEmpty())//
      

  3.   

    role[5,12];
    if(int a = 0; a < role.GetLength(0); a++)
    {
       if(role[a]!=null
    {
    LVItem.SubItems.Add(role[a, b].ToString());
    }
    }