2015-10-01 71 views
-1

我嘗試更新的API我的數據,但我得到一個錯誤:更新API數據錯誤

型「System.NullReferenceException」未處理的異常發生在Api.exe

其他信息:對象引用未設置爲對象的實例。

  string apiCustomerUrl = ApiBaseUrl + "/orgs/" + orgId + "/customers/" + cusid; 
     RootObject customerRow = null; 
     bool rowFound = ConnectApi.GetApiSelectByIdResultRow(apiCustomerUrl, Program.GetApiAccessToken(), out customerRow, out status); 
     HttpStatusCode updateStatusCode = HttpStatusCode.Unused; 
     List<mMApiValidationMessage> validationMessages = null; 

     RootObject updatedRow = null; 
     string titleForUpdate = "New title " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); 
     customerRow.Rows[1].Name = titleForUpdate; 
     bool itemUpdated = ConnectApi.UpdateApiItem(apiCustomerUrl, Program.GetApiAccessToken(), customerRow, out updatedRow, out updateStatusCode); 

我這裏得到一個errror:customerRow.Rows[1].Name = titleForUpdate;

public static bool UpdateApiItem<RootObject>(string apiMethodUrl, string accessToken, RootObject changedRow, out RootObject updatedRow, out HttpStatusCode statusCode) 
    { 
     bool success = false; 
     statusCode = HttpStatusCode.Unused; 
     updatedRow = default(RootObject); 

     HttpContent ct = null; 
     if (changedRow != null) 
     { 
      ct = new StringContent(JsonConvert.SerializeObject(changedRow)); 
      ct.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
     } 


     using (HttpClient httpClient = new HttpClient()) 
     { 
      httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 
      var apiResponse = httpClient.PutAsync(apiMethodUrl, ct).Result; 

      statusCode = apiResponse.StatusCode; 


      var contents = apiResponse.Content.ReadAsStringAsync(); 
      string errJSON = contents.Result; 


      if (apiResponse.IsSuccessStatusCode) 
      { 
       bool changedItemRetrieved = ConnectApi.GetApiSelectByIdResultRow(apiMethodUrl, accessToken, out updatedRow); 

       success = changedItemRetrieved; 
      } 
     } 

     return success; 
    } 

namespace Api 
{ 


    public class Country 
    { 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public string ResourceUrl { get; set; } 
    } 

public class Row2 
{ 
    public int CustomerId { get; set; } 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public string Address { get; set; } 
    public string PostalCode { get; set; } 
    public string City { get; set; } 
    public Country Country { get; set; } 
    public string TaxNumber { get; set; } 
    public string Usage { get; set; } 
} 

public class RootObject 
{ 
    public List<Row2> Rows { get; set; } 
    public int TotalRows { get; set; } 
    public int CurrentPageNumber { get; set; } 
    public int PageSize { get; set; } 
}} 

回答

0

很可能沒有數據customerRow.Rows[1]

您似乎在通過ID檢索客戶,我認爲這會返回1條記錄。因此,您可能需要嘗試使用customerRow.Rows[0]代替。由於列表索引從0開始(這意味着第一條記錄)。

+0

我嘗試,但它的補相同.. – devx

+0

任何人都可以幫助我嗎? – devx

+0

你可以嘗試以下方法嗎? 'RootObject customerRow = new RootObject(); customerRow.Rows =新列表();'在調用api檢索之前。然後更新使用'customerRow.Rows [0] .Name' –

0

你得到bcoz

RootObject customerRow = null; 
customerRow.Rows[1].Name = titleForUpdate; 

的錯誤,因爲你customerRownull你需要使它

RootObject customerRow = new RootObject(); 

這樣GetApiSelectByIdResultRow將在customerRow

+0

我試試 RootObject customerRow = new RootObject(); customerRow.Rows [1] .Name = titleForUpdate; 但我仍然得到相同的錯誤 – devx

+0

你應該嘗試'customerRow.Rows [0] .Name'而不是'1' –

+0

它沒有新的..仍然有錯誤..我真的不知道什麼出了問題 – devx