Reputation: 11
Im new to VB.Net. I am trying to debug my project, but I get a warning: Variable 'csCryptoStream' is used before it has been assigned a value. A null reference exception could result at runtime
How can I fix it?
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
Upvotes: 0
Views: 523
Reputation: 3310
Declare the CryptoStream
as below:
Dim csCryptoStream As CryptoStream = Nothing
Add a 'Case Else
' clause in your Case/Select
Before using this variable check if
If Not IsNothing(csCryptoStream)
' ....
End If
Upvotes: 0