任何人都可以帮我处理下面的代码'system.nullreferenceexception'吗?

时间:2017-02-22 06:02:35

标签: c# .net entity-framework asp.net-mvc-5

public FeeDetails GetFeeDetails(string admissionno)
{
   var feedetails = from f in db.FeeDetails select f;

    FeeDetails feedetail = feedetails.Where(f => f.AdmissionNo == admissionno).FirstOrDefault();
    FeeDetails feedetailfinal = new FeeDetails();

    feedetailfinal.Fee1 = feedetail.Fee1.ToString() != null ? Convert.ToInt32(feedetail.Fee1.ToString()) : 0;
    feedetailfinal.Fee2 = feedetail.Fee2.ToString() != null ? Convert.ToInt32(feedetail.Fee2.ToString()) : 0;
    feedetailfinal.Fee3 = feedetail.Fee3.ToString() != null ? Convert.ToInt32(feedetail.Fee3.ToString()) : 0;
    feedetailfinal.Fee4 = feedetail.Fee4.ToString() != null ? Convert.ToInt32(feedetail.Fee4.ToString()) : 0;
    feedetailfinal.Fee5 = feedetail.Fee5.ToString() != null ? Convert.ToInt32(feedetail.Fee5.ToString()) : 0;


    return feedetailfinal;
}

1 个答案:

答案 0 :(得分:0)

我相信你的每一行应该是这样的:

if(feedetail != null)
{
    feedetailfinal.Fee1 = feedetail.Fee1 != null 
                      ? Convert.ToInt32(feedetail.Fee1) 
                      : 0;
    //other lines
}