Search code examples
vb.netvisual-studio-2017

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


I'm getting an error that saying string does not have an value. but i have assigned a value but somehow there is an error that i cant see. please help.

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

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

when i changed the data type of loginusername & loginpword the error goes away but i need this to be string data types.

        Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
        Dim sr As New IO.StreamReader(strFile)
        Dim ip As String
        Dim port As String
        Dim userid As String
        Dim password As String
        Dim database As String

        'reading
        ip = sr.ReadLine()
        port = sr.ReadLine()
        userid = sr.ReadLine()
        password = sr.ReadLine()
        database = sr.ReadLine()
        sr.Close()


        'default connection code
        Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
        Dim sqlconnection As MySqlConnection = New MySqlConnection
        sqlconnection.ConnectionString = serverstring
        Dim Reader As MySqlDataReader
        Dim loginpword As String
        Dim loginusername As String

        Try
            If sqlconnection.State = ConnectionState.Closed Then
                sqlconnection.Open()
                Dim sqlstatement As String = "SELECT Username,Password FROM SDS_Users WHERE `Username` =' " & Login_tb_username.Text & "'"
                Dim Command = New MySqlCommand(sqlstatement, sqlconnection)
                Reader = Command.ExecuteReader

                While Reader.Read
                    loginpword = Reader.GetString("Password")
                    loginusername = Reader.GetString("Username")

                End While

                Reader.Close()

                If Login_tb_username.Text = loginusername And Login_tb_password.Text = loginpword Then
                    Me.Visible = False
                    Main_window.Visible = True
                    sqlconnection.Close()
                Else
                    MessageBox.Show("Username & Password doesn't match any account!", "Error",
                   MessageBoxButtons.OK)
                End If
            Else
                sqlconnection.Close()
                MsgBox("Connection Error!", "Error")

            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

Solution

  • You are assigning values to your variables in the following code block:

    While Reader.Read
        loginpword = Reader.GetString("Password")
        loginusername = Reader.GetString("Username")
    End While
    

    As the Reader is the result of a database query, it is not guaranteed to contain values. The content of the While block is may not be executed, hence the error you are getting.

    To work around this error, simply initialise the variables with a default/initial value, such as:

    Dim loginpword As String = ""
    Dim loginusername As String = ""