Peter Dzuya

Peter Dzuya

  • 1.3k
  • 313
  • 39.2k

Object reference exception

Jul 16 2015 4:09 AM
Hi Experts,
 
I want to create a new customer whose name already exists from a list of contacts.
Once the customer entity has been created, a receivable and possibly a payable account is also created.
But an exception is thrown with a message "Object reference not set to an instance of an object."
on examining further, I find these messages shown below but I don't know how to solve this problem.
 
'entity.PayableAccount' threw an exception of type 'System.ArgumentNullException'
 'entity.ReceivableAccount' threw an exception of type 'System.ArgumentNullException'
 
 
below is the UI that enables a new customer to be added from a list on contacts
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MakGarm.Business;
using MakGarm.Business.Contracts;
using MakGarm.Common.Entities.Core;
using Microsoft.Practices.Unity;
namespace MakGarm.Start.Customers {
public partial class frmAddCustomer : DevExpress.XtraEditors.XtraForm {
private ICustomerService _customerService;
private Customer _customer;
private IContactService _contactService;
//private Contact _contact;
public frmAddCustomer() {
InitializeComponent();
_customerService = UnityConfig.GetContainer().Resolve<ICustomerService>();
_contactService = UnityConfig.GetContainer().Resolve<IContactService>();
_customer = new Customer() {
Active = true,
Accountclosed = false,
Datecreated = DateTime.Now,
CreditDays = 0,
CreditLimit = 0,
LastEditedDate = DateTime.Now,
CompanyBranchId = CurrentUserInfo.User.CompanyBranch.CompanyId,
CheckerUserId = CurrentUserInfo.User.UserId
};
}
private void frmAddCustomer_Load(object sender, EventArgs e){
customerBindingSource.DataSource = _customer;
ContactBindingSource.DataSource = _contactService.GetAll();
}
private void Acccept_Click(object sender, EventArgs e) {
customerBindingSource.EndEdit();
_customerService.Insert(_customer);
}
}
}
the service (_customerService) that the Acccept_Click uses is shown below.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MakGarm.Business.Contracts;
using MakGarm.Common.Entities.Core;
using MakGarm.Common.Entities.SubsidiaryLedgerAccounts;
using MakGarm.Common.Enums;
using MakGarm.Persistence.Contracts;
using Uwapi.Utils.Data;
namespace MakGarm.Business.Logic {
public class CustomerService : ICustomerService {
private readonly ICustomerRepository _CustomerRepository;
private readonly ICompanyRepository _companyRepository;
private readonly IFinancialYearService _financialYearService;
private readonly IContactRepository _contactRepository;
private readonly IUnitOfWork _unitOfWork;
public CustomerService(
ICustomerRepository customerRepository,
ICompanyRepository companyRepository,
IFinancialYearService financialYearService,
IContactRepository contactRepository,
IUnitOfWork unitOfWork) {
_CustomerRepository = customerRepository;
_companyRepository = companyRepository;
_financialYearService = financialYearService;
_contactRepository = contactRepository;
_unitOfWork = unitOfWork;
}
public void Insert(Customer entity) {
var company = _companyRepository.GetAll().First();
var contact = entity.Contact ?? _contactRepository.GetById(entity.ContactId);
entity.ContactRelationAccounts = new Collection<ContactRelationAccount>() {
new PayableContactRelationAccount() {
OpeningDate = _financialYearService.GetDbServerTime(),// AccountBalance = 0,
Active = true,
Type = SubsidiaryLedgerAccountTypeEnum.CustomerReceivable,
BaseAccountType = BaseAccountTypeEnum.Liability,
SubsidiaryLedgerAccountName = string.Format("{0} (Deposit Holding)", contact.ContactName),
Creator = entity.LastEditor,
OpeningBalance = 0,
Description = string.Format("{0} (Deposit Holding)", contact.ContactName),
IsSystemManaged = true,
ControlGeneralLedgerAccountId = company.CreditorsAccountPayableAccountId,},
new ReceivableContactRelationAccount() {
OpeningDate = _financialYearService.GetDbServerTime(),
AccountBalance = 0,
Active = true,
Type = SubsidiaryLedgerAccountTypeEnum.CustomerReceivable,
BaseAccountType = BaseAccountTypeEnum.Asset,
SubsidiaryLedgerAccountName = string.Format("{0} (Receivable)",contact.ContactName),
Creator = entity.LastEditor,
OpeningBalance = 0,
Description = string.Format("{0} (Receivable)", contact.ContactName),
IsSystemManaged = true,
ControlGeneralLedgerAccountId = company.CustomersAccountReceivableAccountId,
},
};
_CustomerRepository.Add(entity);
_unitOfWork.Commit();
}
public void Update(Customer entity) {
_CustomerRepository.Update(entity);
_unitOfWork.Commit();
}
public Customer GetById(int customerId) {
return _CustomerRepository.GetById(customerId);
}
public void Delete(int customerId) {
var branch = _CustomerRepository.GetById(customerId);
branch.DeletedStatus = true;
_CustomerRepository.Update(branch);
_unitOfWork.Commit();
}
public IEnumerable<Customer> GetAll() {
return _CustomerRepository.GetMany(s => s.DeletedStatus == false);
}
public IEnumerable<Customer> GetByCompanyBranchId(int companyBranchId) {
return _CustomerRepository.GetMany(b => b.CompanyBranchId == companyBranchId);
}
}
}
 
and below is the entity used.
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MakGarm.Common.Entities.Core;
using MakGarm.Common.Entities.Miscellaneous;
using MakGarm.Common.Entities.Documents;
using MakGarm.Common.Entities.SubsidiaryLedgerAccounts;
using MakGarm.Common.Enums;
namespace MakGarm.Common.Entities.Core {
public class Customer : ContactRelation {
#region Primitive Properties
public bool Active { get; set; }
public bool Accountclosed { get; set; }
public DateTime Datecreated { get; set; }
public int CreditDays { get; set; }
public int CreditLimit { get; set; }
[StringLength(255, ErrorMessage = "255 characters max")]
public string Remarks { get; set; }
public DateTime LastEditedDate { get; set; }
#region Foreign Keys
public int CompanyBranchId { get; set; }
public int? CheckerUserId { get; set; }
public int BillingStartFinancialMonthId { get; set; }
#endregion Foreign Keys
#endregion Primitive Properties
#region Navigation Properties
#region Parents
[ForeignKey("BillingStartFinancialMonthId")]
public virtual FinancialMonth BillingStartFinancialMonth { get; set; }
[ForeignKey("CompanyBranchId")]
public virtual CompanyBranch CompanyBranch { get; set; }
[ForeignKey("CheckerUserId")]
public virtual User Checker { get; set; }
#endregion Parents
#region Children
[InverseProperty("Customer")]
public virtual ICollection<CustomerInvoice> CustomerInvoices { get; set; }
[InverseProperty("Customer")]
public virtual ICollection<CustomerReceipt> CustomerReceipts { get; set; }
[NotMapped]public PayableContactRelationAccount PayableAccount {
get {
return ContactRelationAccounts.OfType<PayableContactRelationAccount>().FirstOrDefault();
}
}
[NotMapped]
public ReceivableContactRelationAccount ReceivableAccount {
get {
return ContactRelationAccounts.OfType<ReceivableContactRelationAccount>().FirstOrDefault();
}
}
#endregion Children
#endregion Navigation Properties
}
}
 
 
 
Please assist me. Thanks
 

Answers (4)