c# - Guid.Parse 语句中未处理异常

标签 c# asp.net

我“继承”了一个包含以下代码行的项目:

        objLibPharmacy.UserId = Guid.Parse(Session["GroupId"].ToString());

当我运行调试器时,我收到一条错误消息:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web 
request. Please review the stack trace for more information about the error and where it 
originated in the code. 

部分堆栈跟踪如下:

[NullReferenceException: Object reference not set to an instance of an object.]

UserControl_wuc_Pharmacy.bindPharmacyPopUp()

bindPharmacyPopUp 如下:

  private void bindPharmacyPopUp()
{
    /******************Bind Pharmacy Popup*********************/
    objLibPharmacy = new LibPharmacy();
    objLibPharmacy.PharmacyId = 0;
    objLibPharmacy.UserId = Guid.Parse(Session["GroupId"].ToString());
    objclsPharmacy = new clsPharmacy();
    objDs = objclsPharmacy.GetPharmacy(objLibPharmacy);
    string strFilter = "";
    if (objDs != null)
    {
        if (txtSearchPharmacy.Text != "")
            strFilter = "PharmacyName like '%" + txtSearchPharmacy.Text + "%'";
        DataView dv = objDs.Tables[0].DefaultView;
        if (strFilter != "")
            dv.RowFilter = strFilter;
        Utility.bindGridview(dv.ToTable(), gvPharmacyList);
        Utility.bindDDL(objDs.Tables[1], ddlPharmacyDetail, "Pharmacy");
        //ViewState["PharmacyTable"] = objDs.Tables[0];
    }

    /*********************************************************/
}

是什么导致了空引用?我该如何处理此类空引用才能使调试无误地运行?

最佳答案

如果 Session["GroupId"] 为 null,就会发生这种情况。

您需要在尝试使用它之前检查它。

关于c# - Guid.Parse 语句中未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18132400/

相关文章:

c# - 反序列化具有以数字开头的某些属性名称的json

c# - 快速高效的更新程序

c# - 使用 C# 编辑 Outlook 联系人

c# - 两个模型之间的关系错误在asp.net core 2.1中有更多的键

asp.net - 如何部署 ASP.NET MVC3 应用程序?

asp.net - javascript 和 asp.net 网络用户控件

c# - 无法将字符串转换为 DateTIme

c# - 数据流 TransformManyBlock throttle

c# - WebSetup 可以包含多个 Web 应用程序吗?

asp.net - 开发速度 - Asp.net 与 Silverlight