-1

The code below grabs data based on a provided username and password in VS 2008. When I Check the reader.FieldCount it contains all of the fields it should contain. The issue I am having is at results.Add, which is defined at the top of the file as Dim results As List(Of String). When the program reaches that line, a NullReferenceException is thrown, though I have no idea why.

The program is meant to run on an MC9190-G and works up to that point in the code on the device. I am not using an emulator. the code is deployed directly to the device for testing.

    Public Function getUser(ByVal username As String, ByVal password As String)
    Dim ecif As New EncryptionInfo
    Dim ec As New Encryption(ecif.key, ecif.iv)
    Dim pass_dec, temp As String
    Dim i As Integer
    connect()
    query = "SELECT * FROM UserInfo WHERE Username = '" + username.ToString + "'"
    comm = New SqlCommand(query, cn)
    Dim reader As SqlDataReader = comm.ExecuteReader

    If (reader.FieldCount <> 0) Then
        i = 0
        reader.Read()
        While (i < reader.FieldCount)
            If (i = 1) Then
                pass_dec = reader.GetString(i)
                results.Add = ec.Decrypt(pass_dec)
                Continue While
                i += 1
            End If
            temp = reader.Item(i).ToString
            results.Add(temp)
            i += 1
        End While
        reader.Close()
        cn.Close()
        comm.Dispose()
        Return results
    End If


    MessageBox.Show("No results")
    Return Nothing



    End Function
  • 1
    `results.Add = ec.Decrypt(pass_dec)` – Visual Vincent Oct 01 '16 at 16:46
  • Everything compiles perfectly. Am I missing a new keyword somewhere? I thought I declared everything properly. – Mitchell_Dude Oct 01 '16 at 16:48
  • That line still troubles me, because it shouldn't. Anyway, yes you are missing a `New` keyword somewhere; in the declaration of your list: `Dim results As New List(Of String)`. Inspecting the list with the debugger once the error was thrown would've showed you that it is null. – Visual Vincent Oct 01 '16 at 16:51
  • Thank you very much for helping me. I don't usually use VB and this one caught me. Thanks! – Mitchell_Dude Oct 01 '16 at 17:00

1 Answers1

0

You should use the keyword New:

Dim results As New List(Of String)
Visual Vincent
  • 17,424
  • 5
  • 24
  • 66
peval27
  • 1,011
  • 1
  • 11
  • 35