Thursday, March 22, 2012

Object reference not set to an instance of an object Error

I receive the above error when I run my asp.net Application. I can't see where the mistake is and it's driving me nuts. I'm new to this and any pointing in the right direction is greatly appreciated.

line 58 throws the error

Line 56: If bSwitch Then
Line 57: ' Get the text.
Line 58: EncryptClass.Text = txtSource.Text
Line 59: EncryptClass.Encrypt()
Line 60:

The codebehind for the form is.

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

Dim EncryptClass As EncryptorClass

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' The first time this page is displayed
If Not IsPostBack Then
' Create a new Encryptor object.
EncryptClass = New EncryptorClass
' Store the object in a Session state variable.
Session("EncryptClass") = EncryptClass
Else
' Get the Session EncryptClass variable.
EncryptClass = Session("EncryptClass")
End If

End Sub

Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
' Declare a boolean switch.
Dim bSwitch As Boolean

' Get the value from ViewState and switch it.
bSwitch = Not Viewstate("bSwitch")

' Save the new value in ViewState.
ViewState("bSwitch") = bSwitch

' Use the switch to either Encrypt or restore the text in TextBox1.
If bSwitch Then
' Get the text.
EncryptClass.Text = txtSource.Text
EncryptClass.Encrypt()

' (1) Encrypt it – use the method you developed for the EncryptClass
' (2) Display the text in the textbox
' (3) Change the Button text to say "Restore"
'
txtSource.Text = EncryptClass.Text
btnEncrypt.Text = "Restore"
Else

' (1) Restore the original text – use the method you developed for the EncryptClass
' (2) Display the text in the textbox
'(3) Change the Button text to say "Encrypt"

EncryptClass.Restore()
txtSource.Text = EncryptClass.Text
btnEncrypt.Text = "Encrypt"
End If

End Sub
End Class

Here is the class that I created:

Imports System.Security.Cryptography

Friend Class EncryptorClass
Private mstrText As String
Private mstrOriginal As String

'contols the access to the module-level variables
Public Property Text() As String
Get
Text = mstrText
End Get
Set(ByVal Value As String)
mstrText = Value
' Keep a copy of the original for Restore.
mstrOriginal = Value
End Set
End Property

' Restores Encrypted text back to the original.
Public Sub Restore()
mstrText = mstrOriginal
End Sub

Public Sub Encrypt()

Dim byteArray As Byte()

Dim textEncoder As New System.Text.ASCIIEncoding

'(1) Convert string -> byte array
' use the ASCIIEncoding Class's GetBytes() method to convert the mstrText string, and save the value in the byteArray variable */
byteArray = textEncoder.GetBytes(mstrText)

'(2) Convert byte array to SHA1 hash
Dim result() As Byte
Dim shaM As New SHA512Managed
' use the SHA512Managed Class's ComputeHash() method to convert the byteArray, and save the value in the result variable */
result = shaM.ComputeHash(byteArray)

'(3) Convert byte array to hexadecimal string
Dim hexString As String = ""
Dim i As Integer = 0
Dim intArraySize = result.Length

'Loop thru the "result" Byte Array, and convert each byte into the its hexadecimal value
'The property result.Length will give you the size of the result array, remember that arrays are zero-based.
'To convert a byte into hexadecimal, use the Hex() function.
'Remember to concatenate each value as you convert it, to the previous one in the string.
'When you are done, save the hexString to the mstrText variable, so it can be used by the web form.

While i < intArraySize
hexString = hexString & Hex(result(i))
End While

mstrText = hexString
End Sub
End Class

Thanks,

DigitalDraculaTry changing the following in your else clause of If Not PostBack


EncryptClass = Session("EncryptClass")

to

EncryptClass = CType(Session("EncryptClass"), EncryptorClass)

Kumar,

I changed it but it makes no difference. That code would only be called after subsequent calls. The problem lies in the intial call "IsNotPostback".
Thanks for the assist. I'm still hammering away at it.

Mark
What do you mean the problem lies in the initial call. At what point of time you are getting this error? When the user clicks the button, or when the page loads for the first time?
Are you importing the dll that contains your custom class somewhere?
Kumar,

I'm sorry. The error is thrown after the user clicks the "Encrypt" button.
I meant to say that the problem lies when the object is to be initially instantiated, which occurs on the click event.
It's not even coming up the first time. I anticipate that if the object is successfully instantiated then on the second and subsequent button clicks the session object will be available.
Sorry for any confusion. I'm new to this.

Mark
Martin,

The custom class was creates and is listed in the project solution so I was under the impression that I could just use it. Do I need to import the class, and if so how would I do it, with the "Imports" statement at the top of my webform?
Thanks for the help. I really appreciate it.

Mrk
Well, I only use notepad. So, I am unfamiliar with your IDE. But, the page that uses your class should have a statement something like -

<%@. Register TagPrefix="AnyNameYouLike" NameSpace="TheNamespaceDeclaredInYourClassFile" Assembly="TheNameOfTheCompiledDllWithoutTheDllExtension" %>
Then you use the class either as
<script>Sub someSub()
Dim x as New YourClass()
...
End Sub
</script>
or
<html><body>
<AnyNameYouLike:YourClass runat="server"/>
</body></html>
Cheers
Martin

0 comments:

Post a Comment