c#错误:NullReferenceException未被用户代码处理

本文关键字:用户 代码 处理 错误 NullReferenceException | 更新日期: 2023-09-27 18:15:40

我正在为SAP Business One开发一个工资单附加组件。我尝试选择嵌入在SAP矩阵列单元格中的组合框项,我一直得到一个错误:

NullReferenceException未被用户代码处理:对象引用未设置为对象的实例

我的代码:


public void HandleMenuEvent(ref SAPbouiCOM.MenuEvent pVal)
{
   // Handle Add Menu
   if (pVal.MenuUID == "1282")
   {
       _form.Freeze(true);
       oMatrix.AddRow();
       _edCode.ValueEx = string.Empty;
       _cmbEDDescription = oMatrix.Columns.Item("EDDesc").Cells.Item(oMatrix.RowCount).Specific;
    
       var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
    
       if (_cmbEDDescription.ValidValues.Count > 0)
       {
           // Do nothing
       }
       else
       {
           foreach (var item in earnDeductDescription)
           {
               _cmbEDDescription.ValidValues.Add(item.U_PD_description, string.Empty);
           }
       }
    
       _cmbEDDescription.Select(0, SAPbouiCOM.BoSearchKey.psk_Index);
    
       var edDescValue = string.Empty;
    
       edDescValue = _cmbEDDescription.Value;
    
       var edCode = earnDeductDescription.Where(x => x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
    
       for (int i = 1; i 

The error occurs on the item changed event

#region ItemChanged
if (pVal.ItemChanged && pVal.ColUID == "EDDesc" && pVal.Before_Action == false)
{
    var earnDeductDescription = Program.Kernel.Get().GetAllEarnDeductMasters().Distinct();
    
    var edDescValue = string.Empty;
    
    edDescValue = _cmbEDDescription.Selected.Value;    x.U_PD_description.Trim() == edDescValue.Trim()).Select(y => y.U_PD_code).SingleOrDefault();
    
    for (int i = 1; i 

This is where I attach a user data source to the SAP column

private void BindMatrixToUserDataSource()
{
    // Get main matrix
    oItem = _form.Items.Item("JournalMat");
    oMatrix = oItem.Specific;
        
    _edDescription = _form.DataSources.UserDataSources.Add("EDDesc", SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 30);
    oColumns = oMatrix.Columns;
    _coledDescription = oColumns.Item("EDDesc");
    _coledDescription.DataBind.SetBound(true, "", "EDDesc");
    
    ...some code
}
有谁能帮我解决这个问题吗?

c#错误:NullReferenceException未被用户代码处理

我的建议是_cmbEDDescription.Selected在那一刻是空的,因为在ComboBox中没有选择任何项。你可以这样修改你的代码:

var edDescValue = _cmbEDDescription.Selected == null ? string.Empty : _cmbEDDescription.Selected.Value;