如何解决此错误“ System.NullReferenceException:'对象引用未设置为对象的实例。'”

时间:2019-02-23 06:32:20

标签: asp.net-mvc

我的控制器出现问题,该问题不断返回null属性。下面是我的代码。

  public class Payment1Controller : Controller
  {
    //viewmodel
     private DocumentPayment documentPayment = new DocumentPayment();

     private DocumentPayment LoadDataPaymentV(DocumentPayment model)
     {
        documentPayment = new DocumentPayment();
        var payments = db.paymentVs.ToList();
        var documents = documentRepo.GetList_Documents();
         var queryJoin1 =
            from payment in payments
            from document in documents.Where(w => payment.id== w.id).DefaultIfEmpty(
            select new Payment_TableManualPaymentDto
            {
                ID = payment.ID,
                Name = payment.Name,  
                ContractNumber = payment.ContractNumber,
                ConsumerName = payment.ConsumerName,
            };
            IEnumerable <Payment_TableManualPaymentDto> paymentss = null;

            if (!string.IsNullOrEmpty(model.PaymentDV.searchName))
                queryJoin1 = queryJoin1.Where(x => x.Name.Equals(model.PaymentDV.searchName)); //error is here (System.NullReferenceException: 'Object reference not set to an instance of an object.')
            paymentss = queryJoin1;

            if (queryJoin1 != null && queryJoin1.Count() > 0)
            {
                foreach (Payment_TableManualPaymentDto paymentv in queryJoin1)
                {
                    docuPayment.PaymentDVM.Name = paymentv.Name ;
                    docuPayment.PaymentDVM.ContractNumber = paymentv.ContractNumber ;
                    docuPayment.PaymentDVM.ConsumerName = paymentv.ConsumerName ;

                 docuPayment.PaymentDV.Payment1Details.Add(docuPayment.PaymentDVM); 
                }

            }
     return documentPayment;
     }

    public ActionResult Index(DocumentPayment model) //Payment1ViewModel
    {

        documentPayment = LoadDataPaymentV(model);

        return View(documentPayment);
    }
  } 

当我执行错误时,出现的错误是“ System.NullReferenceException:'参考对象未设置为对象的实例。'”。我在控制器中标记了错误。如何解决该错误。请帮助:)

0 个答案:

没有答案