簡體   English   中英

對象引用未設置為對象的實例-空引用異常

[英]Object reference not set to an instance of an object - Null Reference Exception

if (System.Web.HttpContext.Current.Request.Form[day].ToString() != null)
{
    var test = System.Web.HttpContext.Current.Request.Form[day].ToString();
}

我已經寫了這些行,但是當Form [day]不包含任何值時,它將給出一個null對象異常。 我該如何解決?

您必須先檢查此值,然后才能訪問它。 例如,像這樣:

var form = System.Web.HttpContext.Current.Request.Form;
if (form != null && !String.IsNullOrEmpty(day) && form.AllKeys.Contains(day))
{
    var test = form[day].ToString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM