i write this code in my program to encrypt my files but it showing me warning

Warning 1

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

please guide me. when i run the software it work fine but i want to remove this warning so please help me

Recommended Answers

All 5 Replies

Post your codes and exception.

here is the code

Dim csCryptoStream As CryptoStream
Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
pbStatus.Value =

   Dim csCryptoStream As CryptoStream
            'Declare your CryptoServiceProvider.
            Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
            'Setup Progress Bar
            pbStatus.Value = 0
            pbStatus.Maximum = 100

            'Determine if ecryption or decryption and setup CryptoStream.
            Select Case Direction
                Case CryptoAction.ActionEncrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateEncryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)

                Case CryptoAction.ActionDecrypt
                    csCryptoStream = New CryptoStream(fsOutput, _
                    cspRijndael.CreateDecryptor(bytKey, bytIV), _
                    CryptoStreamMode.Write)
            End Select

            'Use While to loop until all of the file is processed.
            While lngBytesProcessed < lngFileLength
                'Read file with the input filestream.
                intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
                'Write output file with the cryptostream.
                csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
                'Update lngBytesProcessed
                lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
                'Update Progress Bar
                pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
            End While

            'Close FileStreams and CryptoStream.
            csCryptoStream.Close()
            fsInput.Close()
            fsOutput.Close()

0
pbStatus.Maximum = 100

        Select Case Direction
            Case CryptoAction.ActionEncrypt
                csCryptoStream = New CryptoStream(fsOutput, _
                cspRijndael.CreateEncryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)

            Case CryptoAction.ActionDecrypt
                csCryptoStream = New CryptoStream(fsOutput, _
                cspRijndael.CreateDecryptor(bytKey, bytIV), _
                CryptoStreamMode.Write)
        End Select


        While lngBytesProcessed < lngFileLength

            intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)

            csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)

            lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)

            pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
        End While


        csCryptoStream.Close()
        fsInput.Close()
        fsOutput.Close()

this is the warning

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

Have you tried replacing:

Dim csCryptoStream As CryptoStream

With

Dim csCryptoStream As New CryptoStream

What this warning is telling you is that the variable could throw a null reference exception if there is a dependant variable that requires instantiation. By not declaring the variable with the New modifier, you are possibly not creating any dependant arrays, variables, objects, ect....

my application is working fine with this warning so its ok

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.