我嘗試更新的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; }
}}
我嘗試,但它的補相同.. – devx
任何人都可以幫助我嗎? – devx
你可以嘗試以下方法嗎? 'RootObject customerRow = new RootObject(); customerRow.Rows =新列表();'在調用api檢索之前。然後更新使用'customerRow.Rows [0] .Name' –