We help IT Professionals succeed at work.

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

newtoperlpgm asked
Here is my code.
 Public Shared Function GetObsSNames(ByVal strObsStIDs As String) As String
            Dim strWhere As String = Append(strWhere, "ObsSID IN (" & strObsSIDs & ")", " AND ")
            Dim strSql As String = "SELECT * FROM ObsS WHERE " & strWhere
            Dim strSNames As String = ""
            Dim strTitle As String = ""
            Dim objSql As New SqlDataAccess(GetConnectionString(True), strSql, CommandType.Text)
            While objSql.Read
                strTitle = objSql.GetString("Title")
                strSNames = strSNames & " " & strTitle
            End While
            Return strSNames
        End Function
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Dim strWhere As String = Append(strWhere, "ObsSID IN (" & strObsSIDs & ")", " AND ")

Open in new window

strWhere is used in the Append, and that makes no sense.
Change it to

Dim strWhere As String = "ObsSID IN (" & strObsSIDs & ")"

Open in new window

I suspect the original code was building up a complex where clause as the end AND is not needed either
Top Expert 2015

When you say that you have an error, please tell us on which line. We have to spend time on all the code while we could focus directly on the error if we knew where it occurs.

Also, strWhere ends the command (strSql), so the command is incomplete because it terminates abruptly with AND (unless Append adds something else).

Also, what is the type of
Dim objSql As (???) =  New SqlDataAccess(GetConnectionString(True), strSql, CommandType.Text)

Finally, you way of returning the data might be problematic. If a title contains spaces, it will be mixed up with everything else because the titles are separated by spaces in strNames. You should separate the results with a character that you are sure will not appear in any title. The pipe character "|" or a tabulation (vbTab) is often used in these situations.


Changing it to Dim strWhere As String = "ObsSID IN (" & strObsSIDs & ")" worked.