加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 编译器警告:null引用异常

发布时间:2020-12-17 00:26:08 所属栏目:大数据 来源:网络整理
导读:我在Visual Studio 2005中有以下代码. Dim OutFile As System.IO.StreamWriter Try OutFile = New System.IO.StreamWriter(Filename) // Do stuff with OutFile Catch Ex As Exception // Handle Exception Finally If OutFile IsNot Nothing Then OutFile.C
我在Visual Studio 2005中有以下代码.
Dim OutFile As System.IO.StreamWriter
    Try
        OutFile = New System.IO.StreamWriter(Filename)
       // Do stuff with OutFile
    Catch Ex As Exception
       // Handle Exception
    Finally
       If OutFile IsNot Nothing Then OutFile.Close()
    End Try

但VS2005提出警告“If OutFile IsNot ..”这一行

Variable ‘OutFile’ is used before it has been assigned a value. A null reference exception could result at runtime.

有没有办法通过巧妙地改变代码来消除这个警告,或者只是有更好的方法来做我正在尝试做的事情?

谢谢

Dim OutFile As System.IO.StreamWriter
OutFile = Nothing
Try
    OutFile = New System.IO.StreamWriter(Filename)
   // Do stuff with OutFile
Catch Ex As Exception
   // Handle Exception
Finally
   If OutFile IsNot Nothing Then OutFile.Close()
End Try

与C# error: Use of unassigned local variable相似

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读