对象给出空错误

时间:2017-10-17 21:03:57

标签: asp.net asp.net-mvc

我收到此错误 System.NullReferenceException:对象引用未设置为对象的实例。,我知道原因,但我不知道如何解决它。我尝试添加此行

if (postingObj == null) {
                ViewBag.Message = "There is no transaction to be approved";
            }

但也没有奏效;我想抛出一条错误信息。

public ActionResult TransactionList(List<TransactionIssues> postingObj)
    {
        IssueDAO dbObj = new IssueDAO(ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString);
        List<string> docNumbers = new List<string>();

        foreach (var item in postingObj)
        {
            if (item.postTrnx)
            {
                docNumbers.Add(item.docNumber);
            }
        }
        if (docNumbers.Count == 0)
        {
            ViewBag.Message = "Please select at least one Transaction to Approve";
            return View(dbObj.GetAllTransactions());
        }

        dbObj.SetStatus0(docNumbers);
        ViewBag.Message = "Approval Successful!";
        return View(dbObj.GetAllTransactions());
    }

1 个答案:

答案 0 :(得分:0)

这可以帮助你摆脱错误,不知道你的对象,我可以在循环之前建议这个:

if (postingObj == null) {
    postingObj = new List<TransactionIssues>();
}

或者,如果你不依赖于publishObj:

if (postingObj == null) {
    ViewBag.Message = "There is no transaction to be approved";
    return View(dbObj.GetAllTransactions());
}