Jump to content
Xtreme .Net Talk

An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dl


Recommended Posts

Posted

I keep getting this error:

 

An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll

 

Additional information: String reference not set to an instance of a String.

 

It highlights this line:

Return (New UnicodeEncoding).GetBytes(s)

 

Here is the website i found the example at:

http://samples.gotdotnet.com/quickstart/aspplus/default.aspx?url=%2fquickstart%2fhowto%2fdoc%2ffileencrypt.aspx

 

 

Any ideas? :)

 

thanks!

Posted
I don't see that line of code anywhere in that example. Is there a value in "s"? What's the return value of the function this line of code is in? It sounds like "s" is Nothing.
Here's what I'm up to.
Posted

I ment the source code when you click on "view source"

aka this code:

 

Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Class FileEncrypt
   Public Shared Function ConvertStringToByteArray(s As [string]) As [byte]()
       Return (New UnicodeEncoding()).GetBytes(s)
   End Function 'ConvertStringToByteArray


   Public Shared Sub Main()
       Dim fs As New FileStream("EncryptedFile.txt", FileMode.Create, FileAccess.Write)

       'Creating a file stream

       Console.WriteLine("Enter Some Text to be stored in encrypted file:")
       Dim strinput As [string] = Console.ReadLine()
       Dim bytearrayinput As [byte]() = ConvertStringToByteArray(strinput)
       'DES instance with random key
       Dim des As New DESCryptoServiceProvider()
       'create DES Encryptor from this instance
       Dim desencrypt As ICryptoTransform = des.CreateEncryptor()
       'Create Crypto Stream that transforms file stream using des encryption
       Dim cryptostream As New CryptoStream(fs, desencrypt, CryptoStreamMode.Write)
       'write out DES encrypted file
       cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)

       cryptostream.Close()
       'create file stream to read encrypted file back
       Dim fsread As New FileStream("EncryptedFile.txt", FileMode.Open, FileAccess.Read)
       'create DES Decryptor from our des instance
       Dim desdecrypt As ICryptoTransform = des.CreateDecryptor()
       'create crypto stream set to read and do a des decryption transform on incoming bytes
       Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
       'print out the contents of the decrypted file
       Console.WriteLine(New StreamReader(cryptostreamDecr, New UnicodeEncoding()).ReadToEnd())

       Console.WriteLine ()
       Console.WriteLine ("Press Enter to continue...")
       Console.ReadLine()
   End Sub 'Main
End Class 'FileEncrypt


 

thanks

Lee

Posted
OK, after looking at this it appears you probably aren't using this in a Console Application project. Create a new Console Application project and try the code.
Here's what I'm up to.
Posted (edited)
OK' date=' after looking at this it appears you probably aren't using this in a Console Application project. Create a new Console Application project and try the code.[/quote']

 

 

Ahh, yes you were right.. welll I have never used the console application project ...

 

What is wrong with this code:/

 


Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text



Module Module1

   Sub Main()

   End Sub

   Class FileEncrypt
       Public Shared Function ConvertStringToByteArray(ByVal s As [string]) As [byte]()
           Return (New UnicodeEncoding).GetBytes(s)
       End Function 'ConvertStringToByteArray


       Public Shared Sub Main()
           Dim fs As New FileStream("EncryptedFile.txt", FileMode.Create, FileAccess.Write)

           'Creating a file stream

           Console.WriteLine("Enter Some Text to be stored in encrypted file:")
           Dim strinput As [string] = Console.ReadLine()
           Dim bytearrayinput As [byte]() = ConvertStringToByteArray(strinput)
           'DES instance with random key
           Dim des As New DESCryptoServiceProvider
           'create DES Encryptor from this instance
           Dim desencrypt As ICryptoTransform = des.CreateEncryptor()
           'Create Crypto Stream that transforms file stream using des encryption
           Dim cryptostream As New CryptoStream(fs, desencrypt, CryptoStreamMode.Write)
           'write out DES encrypted file
           cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)

           cryptostream.Close()
           'create file stream to read encrypted file back
           Dim fsread As New FileStream("EncryptedFile.txt", FileMode.Open, FileAccess.Read)
           'create DES Decryptor from our des instance
           Dim desdecrypt As ICryptoTransform = des.CreateDecryptor()
           'create crypto stream set to read and do a des decryption transform on incoming bytes
           Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
           'print out the contents of the decrypted file
           Console.WriteLine(New StreamReader(cryptostreamDecr, New UnicodeEncoding).ReadToEnd())

           Console.WriteLine()
           Console.WriteLine("Press Enter to continue...")
           Console.ReadLine()
       End Sub 'Main
   End Class 'FileEncrypt

End Module


 

I know i would have to call the function in:

Sub Main()

 

End Sub

 

but.. i am just not sure how to do that

 

thanks!

Lee

Edited by trend
Posted
The class already has its own Sub Main, you don't need to add another one, just tell the app to use the class's. When the app runs it should prompt you for the location of the Sub Main.
Here's what I'm up to.
Posted
The class already has its own Sub Main' date=' you don't need to add another one, just tell the app to use the class's. When the app runs it should prompt you for the location of the Sub Main.[/quote']

Well crap...

 

I still don't know what you are talking about... And even if I did, I would change the code, run it, see it works, then need to convert it to Windows Application.. so I can use the code in a pre existing program...

 

would the only thing needed to change this code over (besides changing function names and such) would be change : Return (New UnicodeEncoding).GetBytes(s) line?

 

thanks!

Lee

Posted
That line is fine. If you need to convert that code to a Windows program, you need to use another method to get the input from the user (for example - display a form with a textbox and use the text in the textbox as the parameter to ConvertStringToByteArray) instead of using the Console class.
Here's what I'm up to.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...