Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 即使未检查任何内容,仍引发NullReferenceException_Vb.net_Nullreferenceexception_Nothing - Fatal编程技术网

Vb.net 即使未检查任何内容,仍引发NullReferenceException

Vb.net 即使未检查任何内容,仍引发NullReferenceException,vb.net,nullreferenceexception,nothing,Vb.net,Nullreferenceexception,Nothing,我有一些比较简单的代码 Public Overridable Function Comparer(thisValue As Object, otherValue As Object) As Integer Try If thisValue Is Nothing Then If otherValue Is Nothing Then Return 0 Else

我有一些比较简单的代码

    Public Overridable Function Comparer(thisValue As Object, otherValue As Object) As Integer
    Try
        If thisValue Is Nothing Then
            If otherValue Is Nothing Then
                Return 0
            Else
                Return -1
            End If
        Else
            If otherValue Is Nothing Then
                Return 1
            Else
                Return thisValue.ToString.CompareTo(otherValue.ToString)
            End If
        End If
    Catch ex As Exception
        Return 0
    End Try
End Function
try-catch块的原因是:如果该值为Nothing,则在实际的比较行中得到一个NullReferenceException。调试器告诉我这个值是“Nothing”,但无论如何都会落在ELSE分支中

谁能告诉我为什么

更新: 我试图通过插入另一张空头支票来改变这种情况。在我的场景中,这归结为几百个异常,执行速度是可以承受的。但我不想想象有人试图对空列进行排序

这怎么可能?是否还有另一个我不知道的“层次”的虚无。我需要检查thisValue和otherValue的类型吗


该函数永远不会被重写。我已尝试删除“可重写”但没有任何效果。

也许不是
此值是
,而是
.ToString()
没有返回任何内容?请尝试以下代码进行测试:

Public Overridable Function Comparer(thisValue As Object, otherValue As Object) As Integer
    Try
        If thisValue Is Nothing Then
            If otherValue Is Nothing Then
                Return 0
            Else
                Return -1
            End If
        Else
            If otherValue Is Nothing Then
                Return 1
            Else

                Dim thisValueStr As String = thisValue.ToString()
                Dim otherValueStr As String = otherValue.ToString()

                'HERE, CHECK THE TWO STRINGS FOR NULL!!!

                Return thisValueStr .CompareTo(otherValueStr )

            End If
        End If
    Catch ex As Exception
        Return 0
    End Try
End Function

如果是这种情况,请仔细检查正在传递的对象中
ToString()
的实现(假设它是自定义类型)。

很抱歉,我看不出此代码如何传递您所声称的内容。你能发布你正在测试的确切输入条件吗?刚刚插入了一张图片-我觉得我必须证明我的主张:)逻辑上,我相信你的陈述(否则这个问题的重点是什么?!);但我无法复制你的情况。请输入准确的输入条件。请记住,对象是一种非常“特殊”的类型,它允许许多配置(因此也会出现问题)。你的pic看起来很奇怪,但是在说之前我需要知道更多关于这个函数是如何调用的(变量是空的);你能在你的文件上面写上选项Strict并告诉我有什么变化吗?哇。。。我以为“严格”已经“消失”了,所以“永远”在继续。我还有其他事情要处理,但我会在几个小时后再回来更新。该函数是通过对DataTable的列进行排序来独占调用的。捕捉得很好-正如您所说的。我忽略了ToString()实现中的另一个测试(永远不要信任外部数据,即使是您自己创建的;))谢谢!这是否意味着(de)bug将ToString()的结果显示为当前值?(它还会显示什么…)