忽略空引用异常vb.net

时间:2011-08-12 14:29:43

标签: vb.net nullreferenceexception

我在vb.net中编码。 有时数据为空/空,这是由于用户输入数据库。 我想绕过它,但我没有运气。

这是我的代码片段:

If hct.mydbvalue.name IsNot Nothing Then
   dr("mydbvalue") = hct.mydbvalue.name
End If

我仍然收到错误:

System.NullReferenceException:未将对象引用设置为对象的实例。

如果它是一个空值而没有做任何事情吗?

11 个答案:

答案 0 :(得分:2)

@FishBasketGordo和@Yuck都是正确的,您需要检查可为空的完整对象路径:

If (hct IsNot Nothing) AndAlso (hct.mydbvalue IsNot Nothing) AndAlso (hct.mydbvalue.name IsNot Nothing) Then
   dr("mydbvalue") = hct.mydbvalue.name
End If

答案 1 :(得分:1)

使用ADO.NET时,数据库中的数据不会得到NullReferenceException; ADO.NET使用DBNull.Value来表示null,因此您的null引用来自其他地方。在上面的代码中,如果以下任何一个为null,则可能发生异常:

  • hct
  • hct.mydbvalue
  • dr

答案 2 :(得分:0)

确保整个链不为空。如果hctmydbvalue为空,您将获得例外。

答案 3 :(得分:0)

您应该检查hctNothing还是mydbvalue如果您查看异常消息属性,它将告诉您导致错误的原因。

答案 4 :(得分:0)

对我而言,hct.mydbvalue似乎为空,因此您无法在其上调用“名称”。

答案 5 :(得分:0)

Private Function NullCheck(ByVal inObject As Object, Optional ByVal defaultValue As Object = "") As Object
    Dim out As Object
    If Not IsDBNull(inObject) Then
        out = inObject ' This returns the value that was passed in when it is not null
    Else
        out = defaultValue ' This ensures that out is something and defaults to ""
    End If
    Return out
End Function

答案 6 :(得分:0)

我也在解决这个问题,但在C#中。

在我的项目中,我们有复杂的对象路径,如“RootObject.childObject.LitleObject.TinyObject.StringName”

当路径中的任何这些对象为null时,当你尝试像

这样的东西时,你会得到一个空引用

if(RootObject.childObject.LitleObject.TinyObject.StringName ==“a”)

如果它只是工作,我会没事的,因为整个路径的其余部分都是空的。

例如。当childObject = null时,我还希望RootObject.childObject.LitleObject.TinyObject.StringName为null,而不是null引用异常。

但是我还没有找到任何解决方案,但是有一个新的运算符可以在某些空任务中稍微帮助你。

a = object.object ??默认值;

操作员?就像SQL Server中的ISNULL一样。如果左边的对象为null,则从右边返回对象。

它还取代了迈克尔上面发布的整个功能NullCheck。

希望这会有所帮助。

有关运营商的更多信息 http://msdn.microsoft.com/en-us/library/ms173224(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/6a71f45d(V = VS.80)的.aspx

答案 7 :(得分:0)

你在谈论不同的事情。

使用ISDBNull(x.y),String.IsNullOrEmpty(x.y)或(x.y = null)无关紧要

问题比调用所选函数要早得多。

当X为null时,它不能具有属性Y.所以当你调用

AnyOfYourPrefferedFunctions(x.y);

在评估x.y期间出现错误(null。不存在),因此它在机器实际知道您要调用的函数之前停止。

可能只有检查方法才会使用反射。但是你需要发送带有路径的字符串和对root的引用。所以像:

var v = GetValueThroughReflection(rootObject, "rootObject.path.to.the.last.object");

然后,您将能够编写一个函数,该函数将遍历对象路径并找到哪个为null并相应地处理它。例如返回null。

但是当你大量使用该功能时,它会使你的应用程序运行得更慢。因为你将使用反射来完成这样一个简单的任务,就像从变量中获取价值一样。

答案 8 :(得分:0)

从VS14 +

你可以使用

If hct?.mydbvalue?.name IsNot Nothing Then dr("mydbvalue") = hct.mydbvalue.name End If

答案 9 :(得分:-1)

尝试插入IF NOT isdbnull(hct.mydbvalue.name)

答案 10 :(得分:-1)

以下代码检查所有值。

If hct IsNot Nothing AndAlso 
   hct.mydbvalue IsNot Nothing AndAlso
   Not String.IsNullOrWhitespace(hct.mydbvalue.name) Then

    dr("mydbvalue") = hct.mydbvalue.name

End If

注意最后一次测试使用了String.IsNullOrWhitespace()。我假设name是一个字符串,你不想保存空字符串。

更新1

以下代码是一个简单的控制台应用程序,用于证明当hct.mydbvalue为Nothing时,使用IsDbNull()或Micheal的NullCheck()将抛出NullReferenceException。

Module Module1

    Sub Main()

        Dim hct = New hct
        Dim dr = New Dictionary(Of String, String)
        Dim errorCount = 0

        Try
            Dim thisCallWillFail = IsDBNull(hct.mydbvalue.name)

        Catch ex As NullReferenceException
            Console.WriteLine(
                "Using IsDBNull() threw NullReferenceException as expected."
            )
            errorCount += 1

        End Try

        Try
            Dim thisCallWillFail = NullCheck(hct.mydbvalue.name)

        Catch ex As NullReferenceException
            Console.WriteLine(
                "Using NullCheck() threw NullReferenceException as expected."
            )
            errorCount += 1

        End Try

        Console.WriteLine("errorCount = 2? {0}", errorCount = 2)

    End Sub

    Private Function NullCheck(ByVal inObject As Object, 
                               Optional ByVal defaultValue As Object = "") As Object

        Dim out As Object

        If Not IsDBNull(inObject) Then

            ' This returns the value that was passed in when it is not null
            out = inObject 

        Else

            ' This ensures that out is something and defaults to ""
            out = defaultValue 

        End If

        Return out

    End Function

End Module

Public Class hct
    Property mydbvalue As mydbvalue
End Class

Public Class mydbvalue
    Property name As String
End Class