简体   繁体   中英

Calling webservice results in error "Object reference not set to an instance of an object"

I call a specific webservice method from my android phone app and it always results in the error:

Object reference not set to an instance of an object

I call MANY different methods in this service with no problem, this is the only one that gives me an issue and I cannot track it down. The webservice is written in VB.NET , the phone app C# . Breakpoints confirm that all the data is being passed correctly, there is NO null data.

I created a "log" system where I can log messages to my database at certain places in my code. The messages are logged in the database, but the method, apparently, errors out on a call to a function within the method. I setup the webservice to be able to test it in the production environment remotely and it works EVERYTIME, yet when I call it from my phone app, I get the error. I'm stumped. This is the error message I get:

Source: System.Web.Services Description:Server was unable to process request. Object reference not set to an instance of an object. Target Site:System.Object[] ReceiveResponse(System.Net.WebResponse, System.Web.Services.Protocols.SoapClientMessage, System.Web.Services.Protocols.SoapExtension[]) Call Stack: at System.Web.Services.Protocols.SoapHttpClientProtocol.ReceiveResponse (System.Net.WebResponse response, System.Web.Services.Protocols.SoapClientMessage message, System.Web.Services.Protocols.SoapExtension[] extensions) [0x001ed] in <6f5d26adf5754fd8a1fe9ebdbdc88f48>:0 at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke (System.String method_name, System.Object[] parameters) [0x000ad] in <6f5d26adf5754fd8a1fe9ebdbdc88f48>:0 at MyAndroidApp.DataInterfaceWeb.DataInterface.ProcessSubscription (System.String list1, System.String list2, System.String list3, System.String list4, System.String list5, System.String list6, System.String list7, System.String list8, System.String list9, System.String list10, System.String list11, System.String list12, System.String list13, System.String list14, System.String lis t15, System.String list16, System.String list17, System.String list18, System.String list19) [0x00001] in C:\\Users\\HP\\source\\repos\\Projects\\MyAndroidApp\\MyAndroidApp\\Web References\\DataInterfaceWeb\\Reference.cs:2308 at (wrapper remoting-invoke-with-check) MyAndroidApp.DataInterfaceWeb.DataInterface.ProcessSubscription(string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string) at MyAndroidApp.Subscription+<>c__DisplayClass24_0.b__1 (System.Object sender, System.EventArgs e) [0x001ee] in C:\\Users\\HP\\source\\repos\\Projects\\MyAndroidApp\\MyAndroidApp\\Subscription.cs:211

This is the Web method:

<WebMethod()>
Public Function ProcessSubscription(ByVal list1 As String, ByVal list2 As String,
                               ByVal list3 As String, ByVal list4 As String,
                               ByVal list5 As String, ByVal list6 As String,
                               ByVal list7 As String, ByVal list8 As String,
                               ByVal list9 As String, ByVal list10 As String,
                               ByVal list11 As String, ByVal list12 As String,
                               ByVal list13 As String, ByVal list14 As String,
                               ByVal list15 As String, ByVal list16 As String,
                               ByVal list17 As String, list18 As String, list19 As String) As String
    Dim LogErrors As String = ConfigurationManager.AppSettings("LOG")
    Dim MyResponse As ANetApiResponse
    If LogErrors = "TRUE" Then
        LogError("Processing Subscription") '<-- this line is executed as this message appears in the database
    End If
    Try
        Dim amount As Decimal = Convert.ToDecimal(list16)
        ' I believe that this is the call that causes the error, yet calling 
        ' the method directly via the browser works every time!  Just won't
        ' work when I call the method from the android app.
        MyResponse = ChargeCreditCard.Run(amount.ToString, list11, list12, list19, list3, list4, list6, list7, list8, list9)
    Catch ex As Exception
        LogError(ex.Message) ' <--- never hits this line as nothing is logged after the "Processing subscription" log message
    End Try
    Dim returnmessage As String
    If MyResponse.messages.resultCode = messageTypeEnum.Ok Then
        returnmessage = "SUCCESSFUL"
        PaySubFee(list1, list2, list13)
    Else
        returnmessage = "The card was declined: " & "Error Code: " & MyResponse.messages.message(0).code
    End If

    Return returnmessage 'MyResponse.messages.resultCode ' 0 = ok, 1 = error

    Return result
End Function

What am I missing?

No anyone could help you in this particular error without code and troubleshooting it! This error when happening that there is an object which is not initialized correctly most of the time.

After several days and hours on end of testing I was able to resolve the issue. I am rather new at developing android apps, so certain things tend to stick in my mind and one of those things is that android for some reason tends to add an extra space, albeit randomly it seems, and depending on the situation, it can cause problems. On a whim I decided to trim ALL the data that was being transmitted on the call to the webservice and it immediately began working as designed. A review of my logs did not reveal any extra spaces in the data, but none the less that appears to be the case here. No where in Authorize.NET's support documentation or forums is there any mention that extra spaces would cause issues, but it does. If there is leading (or trailing?) spaces in the transaction submitted, controller.GetApiResponse() WILL (apparently) return a null response. Hopefully this will help someone else who is experiencing the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM