2011-11-24 91 views
0

我正在开发用于SAP业务的工资单加载项1.我不断收到错误: "NullReferenceException was unhandled by user code :Object reference not set to an instance of an object."当我尝试选择嵌入SAP矩阵列单元格中的组合框项目。C#错误: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

的项目更改事件

 
#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

出现的错误。这就是我重视用户数据源到SAP列

 
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 
} 

任何人都可以帮我解决这个问题吗?

+2

“_cmbEDDescription”为null或_cmbEDDescription.Selected为。通过代码来找出哪些以及为什么。 –

+1

除了你面临的问题之外,它看起来像你不确定'ref'的意思,因为你在第一种方法中并不真正使用它。请阅读http://pobox.com/~skeet/csharp/parameters.html –

+0

感谢Jon Skeet,你一如既往的乐于助人。伟大的文章。 –

回答

1

我的建议是_cmbEDDescription.Selected此时为空,因为在ComboBox中没有选择任何项目。您可能会更改您的代码:

var edDescValue = _cmbEDDescription.Selected == null ? string.Empty : _cmbEDDescription.Selected.Value; 
相关问题