NullReference异常未被用户代码处理

时间:2016-02-25 15:19:15

标签: vb.net null

我不明白为什么我会收到以下错误:

NullReference Exception was unhandled by user code
Object reference not set to an instance of an object.

来自以下代码:

Dim repNumber As String

 If Not (gridView.GetFocusedRowCellValue("RepNo").ToString.Trim = Nothing) Then
            repNumber = gridView.GetFocusedRowCellValue("RepNo").ToString.Trim
 Else
            MessageBox.Show("repNumber is empty!")
            repNumber = repCurrent
 End If

这条线在做什么? (验证值是否为NULL(或NOT NULL)):

If Not (gridView.GetFocusedRowCellValue("RepNo").ToString.Trim = Nothing) Then

(上一行是给我错误的那一行)

1 个答案:

答案 0 :(得分:-1)

" NULL"并且"没什么"不是一回事....你需要这样做。

Dim repNumber As String

     If (gridView.GetFocusedRowCellValue("RepNo") is dbnull.value) Then
           MessageBox.Show("repNumber is empty!")
           repNumber = repCurrent 
     Else
           repNumber = gridView.GetFocusedRowCellValue("RepNo").ToString.Trim
     End If

"没有什么"是指尚未实例化的对象...." Dbnull.value"是指从数据库中读取的空值....