gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:17:22 26 4
gpt4 key购买 nike

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

        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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com