-2
Function MD5Hash(ByVal values As String) As Byte()
    Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(values))
End Function

Function Encrypt(ByVal Stringinput As String, ByVal key As String) As String
    des.Key = MD5Hash(key)
    des.Mode = CipherMode.ECB
    Dim buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(Stringinput)
    Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
End Function


Private Sub btnSignup_Click(sender As Object, e As EventArgs) Handles btnSignup.Click
    Using con As New SQLiteConnection(ConnectionString.ToString)

    Dim query As String = "INSERT INTO LoginTB (user, password) VALUES (@username,@password)"
    con.Open()
        Using cmd As New SQLiteCommand(query, con)

            cmd.Parameters.AddWithValue("@username", TxtBxUsername.Text)
            cmd.Parameters.AddWithValue("@password", Encrypt(TxtBxPassword.Text, "abc"))
            cmd.ExecuteNonQuery()
            MsgBox("user created successfully")
            con.Close()
        End Using
    End Using
End Sub

The error appears in the value at MD5Hash when I click the signup button.

System.NullReferenceException 'Object reference not set to an instance of an object.'

I know it has been asked a lot. But I couldn't find a solution to this issue.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Declare `Encrypt` and `MD5Hash` as [`Shared`](https://stackoverflow.com/q/613998/1115360) methods, i.e. `Shared Function MD5Hash(ByVal values As String) As Byte()` etc. – Andrew Morton Apr 14 '19 at 16:25
  • Set a breakpoint on the code line producing the error and inspect the variables to see which one is `Nothing`. Initialize this variable. – Olivier Jacot-Descombes Apr 14 '19 at 16:35
  • when i declare it as share, it shows me `Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.` – qwerttrree Apr 14 '19 at 16:45

1 Answers1

0

The issue may caused due to instance is not created for MD5. Can you try change your code for the function MD5Hash as below?

Function MD5Hash(ByVal values As String) As Byte()
    Static hash As MD5 = MD5.Create()
    Return hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(values))
End Function
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Local variable cannot have the same name as the function containing it. GETTING THIS ERROR – qwerttrree Apr 14 '19 at 16:56
  • 1
    **Inside the function**, change `md5Hash` to something else, like just `hash`, **in both places**, leaving the function name the same. I'd also change `Dim` to `Static` so the function reuses the same instance each time. – Idle_Mind Apr 14 '19 at 17:07