从sqlserver数据库中读取image字段到byte[],
在另一函数中需判断该数组是否赋值,
if( _bytephoto == null | _bytephoto.Length == 0 )
{
}
这样不行,提示“未定义值”
未处理的“System.NullReferenceException”类型的异常出现在 qqproject.model.dll 中。其他信息: 未将对象引用设置到对象的实例。
---------
怎么判断?

解决方案 »

  1.   

    我的没问题:) private bool IsNull( byte[] inByte )
    {
    if ( inByte.Length == 0 || null == inByte )
    return true ; 
    else
    return false ; 
    }
      

  2.   

    楼主
    if( _bytephoto == null | _bytephoto.Length == 0 )
    {
    }
    这里存在逻辑错误1) 逻辑OR操作是"||",不是"|"
    2) 当_bytephoto = null,时,你却计算_bytephoto.Length == 0??肯定出错,改如下bool IsEmpty = false;if( _bytephoto != null )
    {
       if (_bytephoto.Length == 0 )
       {
          //空数组
          IsEmpty = true;
       }
    }
    else
    {
         IsEmpty = true;
    }if (IsEmpty)
    {
    //这里处理是空的情况
    }
      

  3.   

    private bool IsNull( byte[] inByte )
    {
    if ( inByte.Length == 0 || null == inByte )
    return true ; 
    else
    return false ; 
    } private void button1_Click(object sender, System.EventArgs e)
    {
    byte[] b = null;
    MessageBox.Show(IsNull(b).ToString());
    }
    报错啊!!一样的。
      

  4.   

    1楼的和楼主一样的错误,改成下面简单的表达式bool isEmpty =  (_bytephoto != null) ? (_bytephoto.Length ==0) : true;