【问题标题】:SOAP Request error... why am I getting a NRE?SOAP 请求错误...为什么我会收到 NRE?
【发布时间】:2011-04-17 14:27:34
【问题描述】:

我几乎从 API PDF 中复制并粘贴了代码,但在线上得到了 Null 引用异常:

AccountArray(0) = AccountToUpdate

有人知道我做错了什么吗?谢谢!

Dim boolQueryFinished = False
Dim strCurrentID As String = "0"
Dim contractid As Integer
Dim strWebURI As String = "https://webservices.autotask.net/atservices/1.5/atws.asmx"
Dim myService As New ATWS
myService.Url = strWebURI
Dim cred As New System.Net.NetworkCredential("U", "P")
Dim credCache As New System.Net.CredentialCache
credCache.Add(New Uri(myService.Url), "Basic", cred)
myService.Credentials = credCache
Dim AccountID As New ArrayList
Dim i As Integer
While Not (boolQueryFinished)
Dim strQuery As String = "<queryxml><entity>Contract</entity>" & _
"<query><condition><field>id<expression op=""greaterthan"">" & strCurrentID &
"</expression></field></condition></query>" & _
"</queryxml>"
Dim r As ATWSResponse
r = myService.query(strQuery)
If r.EntityResults.Length > 0 Then
    For Each ent As Entity In r.EntityResults 
        strCurrentID = ent.id
        AccountID.Add(CType(ent, Contract).AccountID)

    Next
Else
    boolQueryFinished = True
    While i < ContractName.Count
        listOutput.Items.Add(ContractName(i))
        listOutput.Items.Add(id(i))
        If ContractName(i).Contains("Georges") Then
            listOutput.Items.Add("Found it")
            Dim sResponse As ATWSResponse
            Dim AccountToUpdate As New Contract
            Dim AccountArray(0) As Contract
            AccountToUpdate.AccountID = AccountID(i)


            AccountArray(0) = AccountToUpdate
            Dim entityArray() As Entity = CType(AccountArray, Entity())
            sResponse = myService.update(entityArray)

            If sResponse.ReturnCode = 1 Then
                listOutput.Items.Add("Updated")
            Else
                listOutput.Items.Add("Failed")

            End If
        End If

        i = i + 1
    End While
    listOutput.Items.Add(id.Count)
    listOutput.Items.Add(ContractName.Count)
End If
End While

【问题讨论】:

  • 您需要发布整个else 块,直到End While

标签: .net asp.net xml vb.net soap


【解决方案1】:

看来:

  • r.EntityResults.Length 不大于零。你陷入了else 子句。
  • 进入了While i &lt; ContractName.Count 循环(但不清楚这个ContractName 是什么。
  • i 还没有初始化,所以它的值是0
  • 根据我们在上面发布的内容,我们应该看到 ArgumentOutOfRange Exception,因为我们正试图获取空 ArrayList 的索引 0。


AccountToUpdate.AccountID = AccountID(i)

您确定要在Else 块中包含While 循环吗?我很好奇End If 声明在哪里?

【讨论】:

  • 我添加了其余部分。 1. 如果查询成功并继续运行并将值添加到数组中,则 r.EntityResults.Length 大于 0:AccountID.Add(CType(ent, Contract).AccountID)。一旦它达到 0,它就会停止,Else 会启动并组合一个响应并尝试将其发送回来。
  • ContractName 也是一个数组,它在 r.EntityResults.Length IF 中获取添加信息
  • 呃,我拼写了一个变量名 wearg。明白了,谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 2013-08-11
  • 2013-04-07
  • 1970-01-01
  • 2020-11-05
相关资源
最近更新 更多