You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Acumatica API PUT更新库存项时出现NullReference错误

Acumatica PUT API更新库存项时NullReference错误排查

问题详情

调用PUT API更新库存项时返回500错误,请求地址:

https://.../entity/Default/18.200.001/StockItem?$filter=InventoryID eq '010343914117'

错误响应内容:

{
    "message": "An error has occurred.",
    "exceptionMessage": "Operation failed",
    "exceptionType": "PX.Data.PXInvalidOperationException",
    "stackTrace": "   at PX.Api.ContractBased.EntityService.Put(ISystemContract systemContract, String version, String name, EntityImpl entity, CbOperationContext operationContext, Boolean throwOnError)\r\n   at PX.Api.ContractBased.Soap.SoapFacadeBase.PutImpl(EntityImpl entity, Boolean throwOnValidationError)\r\n   at PX.Api.ContractBased.SystemContracts.V2.RestController.PutEntity(EntityImpl entity, ...",
    "innerException": {
        "message": "An error has occurred.",
        "exceptionMessage": "Object reference not set to an instance of an object.",
        "exceptionType": "System.NullReferenceException",
        "stackTrace": "   at KN.CP.KNCPInventoryItemMaintExt.InventoryItem_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected BaseSelectedevent)\r\n   at KN.DistrictCameras.KNDCInventoryItemMaintBaseExt.InventoryItem_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected BaseEvent)\r\n   at KN.S3Images.KNCPInventoryItemMaintS3Ext.InventoryItem_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected BaseSelectedevent)\r\n   at KNCommerceBasic.KNCBInventoryItemMaintExt.InventoryItem_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected BaseEvent)\r\n   at KNMagentoConnector.KNMCInventoryItemMaintExtn.InventoryItem_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected BaseEvent)\r\n   at PX.Data.PXRowSelected.Invoke(PXCache sender, PXRowSelectedEventArgs e)\r\n   at PX.Data.PXCache.OnRowSelected(Object item)\r\n   at PX.Api.SyImportProcessor.SyStep.CommitChangesInt(Object itemToBypass, PXFilterRow[] targetConditions, PXFilterRow[] filtersForAction, SyImportRowResult importResult)\r\n   at PX.Api.SyImportProcessor.SyStep.CommitChanges(Object& itemToBypass, PXFilterRow[] targetConditions, PXFilterRow[] filtersForAction)\r\n   at PX.Api.SyImportProcessor.ExportTableHelper.ExportTable()"
    }
}

注:使用相同地址执行GET请求可正常获取该库存项记录。

错误原因

从堆栈跟踪可以明确,错误源于自定义扩展的RowSelected事件处理逻辑中的空引用:

  • 多个自定义扩展(KN.CP.KNCPInventoryItemMaintExt、KN.DistrictCameras.KNDCInventoryItemMaintBaseExt等)的InventoryItem_RowSelected方法在执行时,访问了未实例化的对象(即某个变量为null但直接调用了其属性/方法)。
  • GET请求仅读取数据,不会触发完整的业务逻辑校验和扩展事件;而PUT更新操作会触发RowSelected这类数据绑定/校验事件,当扩展代码未做空值判断时,就会抛出NullReferenceException。
  • 可能的触发场景:PUT提交的JSON缺失了自定义扩展逻辑依赖的字段,或者更新上下文与GET读取时的上下文存在差异,导致扩展代码中引用的关联对象为null。

修复建议

  1. 检查自定义扩展代码:针对堆栈中列出的所有InventoryItemMaintExt扩展,逐一排查RowSelected方法的逻辑,添加必要的空值判断(比如if (obj != null)),避免直接访问null对象的属性。
  2. 对比GET与PUT的JSON数据:将GET返回的完整库存项JSON与PUT提交的JSON对比,确保PUT数据包含了自定义扩展依赖的所有必要字段,避免因字段缺失导致关联对象无法实例化。
  3. 测试完整数据更新:先使用GET获取的完整JSON修改后提交PUT请求,验证是否能正常更新;如果正常,再逐步减少字段,定位到具体导致错误的缺失字段。

内容的提问来源于stack exchange,提问作者Peter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:17:23