警告变量 'csCryptoStream' 在赋值之前被使用。空引用异常可能会在运行时产生

Warning Variable 'csCryptoStream' is used before it has been assigned a value. A null reference exception could result at runtime

我是 VB.Net 的新手。我正在尝试调试我的项目,但收到警告:变量 'csCryptoStream' 在被赋值之前已被使用。运行时可能会导致空引用异常

我该如何解决?

Private Sub EncryptOrDecryptFile(strInputFile As String, strOutputFile As String, bytKey As Byte(), bytIV As Byte(), Direction As crypt.CryptoAction)
    ' The following expression was wrapped in a checked-statement
    Try
        Me.fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
        Me.fsOutput = New FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
        Me.fsOutput.SetLength(0L)
        Dim bytBuffer As Byte() = New Byte(4096) {}
        Dim lngBytesProcessed As Long = 0L
        Dim lngFileLength As Long = Me.fsInput.Length
        Dim cspRijndael As RijndaelManaged = New RijndaelManaged()
        Me.pbStatus.Value = 0
        Me.pbStatus.Maximum = 100
        Dim csCryptoStream As CryptoStream
        Select Case Direction
            Case crypt.CryptoAction.ActionEncrypt
                csCryptoStream = New CryptoStream(Me.fsOutput, cspRijndael.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write)
            Case crypt.CryptoAction.ActionDecrypt
                csCryptoStream = New CryptoStream(Me.fsOutput, cspRijndael.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
        End Select
        While lngBytesProcessed < lngFileLength
            Dim intBytesInCurrentBlock As Integer = Me.fsInput.Read(bytBuffer, 0, 4096)

Warning --->     csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
            ' The following expression was wrapped in a unchecked-expression
            lngBytesProcessed += CLng(intBytesInCurrentBlock)
            ' The following expression was wrapped in a unchecked-expression
            Me.pbStatus.Value = CInt(Math.Round(CDec(lngBytesProcessed) / CDec(lngFileLength) * 100.0))
        End While
  1. 声明 CryptoStream 如下:

     Dim csCryptoStream As CryptoStream = Nothing
    
  2. Case/Select

  3. 中添加“Case Else”子句
  4. 在使用这个变量之前检查是否

    If Not IsNothing(csCryptoStream)
      '    ....
    End If