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

C# Guid.Parse语句中未处理异常,c#,asp.net,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

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

        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.]
  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];
    }

    /*********************************************************/
}
UserControl\u wuc\u Pharmacy.bindPharmacyPopUp()

BindPharmacypop如下所示:

[NullReferenceException: Object reference not set to an instance of an object.]
  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];
    }

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

是什么导致空引用?如何处理这种空引用,以便调试运行时不会出错?

如果
会话[“GroupId”]
为空,则会发生这种情况


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

如果
会话[“GroupId”]
为空,则会发生这种情况

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

顾名思义,当您尝试对未初始化或已取消引用的对象执行操作时,会发生。在本例中,您正在
会话[“GroupId”]
上调用
.ToString()
,该会话可能尚未初始化

最好的做法是在访问GroupId会话变量之前初始化它。作为一种解决方法,如果变量为null,则可以跳过解析:

if (Session["GroupId"] != null)
{
    objLibPharmacy.UserId = Guid.Parse(Session["GroupId"].ToString());
}
顾名思义,当您尝试对未初始化或已取消引用的对象执行操作时,会发生错误。在本例中,您正在
会话[“GroupId”]
上调用
.ToString()
,该会话可能尚未初始化

最好的做法是在访问GroupId会话变量之前初始化它。作为一种解决方法,如果变量为null,则可以跳过解析:

if (Session["GroupId"] != null)
{
    objLibPharmacy.UserId = Guid.Parse(Session["GroupId"].ToString());
}

您可以尝试
Guid.TryParse
方法:

Guid userId;
if (Guid.TryParse(Session["GroupId"].ToString(), out userId))
   objLibPharmacy.UserId = userId;

您可以尝试
Guid.TryParse
方法:

Guid userId;
if (Guid.TryParse(Session["GroupId"].ToString(), out userId))
   objLibPharmacy.UserId = userId;