Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi -

I have a web method calling a stored procedure. The sp works when run separately. Now moving that into the method I am getting a null reference exception which i'm not sure how to resolve. I'm not to familiar with working with service so any help is appreciated. The idea is to select a list of ID's from the sp via the web method. Invoking the web method is when the error is thrown.

Here is the code
<WebMethod()>
Public Function GetPTSID(term As String)
    Dim ptsid As List(Of String)

    Dim cs As String = ConfigurationManager.ConnectionStrings("PTS_EConnectionStringDEV").ConnectionString
    Using con As New SqlConnection(cs)

        Dim cmd As New SqlCommand("stpr_GetPTSIDs", con)
        cmd.CommandType = CommandType.StoredProcedure
        Dim parm As New SqlParameter("term", term)
        cmd.Parameters.Add(parm)
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()

        While reader.Read()
            ptsid.Add(reader("TAPV_TAPNUM"))
        End While
    End Using

    Return ptsid

End Function
Posted
Comments
[no name] 30-Jun-15 12:05pm    
A null reference where?
Troy Bryant 30-Jun-15 12:07pm    
System.NullReferenceException: Object reference not set to an instance of an object.
at ParkerTapWeb.WebService1.GetPTSID(String term) in C:\Users\518563\Documents\ParkerPTSWeb\DAL\WebService1.asmx.vb:line 60

Line 60 which is ptsid.add(reader("tapv_tapnum"))

1 solution

The variable you're using to hold your List collection is Nothing (null).

When you declared it like this:
Dim ptsid As List(Of String)

you only said that the variable ptsid CAN hold a List(Of String). You didn't actually create the List.

Change that to
Dim ptsid As New List(Of String)

and you'll create the list.

This one would be really easy to find if you understood how to use the debugger.
 
Share this answer
 
v2
Comments
Troy Bryant 30-Jun-15 14:22pm    
Unfortunately the first time I ran this with the NEW list it threw this error :

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.
[no name] 30-Jun-15 15:06pm    
I do not believe that is related. That error is probably because you are trying to return a list from a function that is defined as returning nothing.
Dave Kreskowiak 30-Jun-15 15:10pm    
OK, that's a different problem. Is this error on the client or the web service code?

Oh, by the way, your function is defined as not returning anything.

My guess is on the client side.
Troy Bryant 30-Jun-15 15:47pm    
the issue is on the web service when trying to invoke the method
Dave Kreskowiak 30-Jun-15 16:12pm    
Well, then its because your Function declaration doesn't say what it's returning so it returned Nothing (null).

Public Function GetPTSID(term As String) As List(Of String)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900