这个错误是什么意思?,我一直得到这个错误,它用来工作正常,它刚刚开始抛出这个错误....任何帮助?
img1.ImageUrl = ConfigurationManager.AppSettings.Get("Url").Replace("###", randomString)
+ Server.UrlEncode(((System.Web.UI.MobileControls.Form)Page.FindControl("mobileForm")).Title);
Run Code Online (Sandbox Code Playgroud)
MyProject.DLL中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理
附加信息:未将对象引用设置为对象的实例.
它意味着您的调用链中的某个位置,您尝试访问属性或调用对象上的方法null.
鉴于你的陈述:
img1.ImageUrl = ConfigurationManager
.AppSettings
.Get("Url")
.Replace("###", randomString)
+ Server.UrlEncode(
((System.Web.UI.MobileControls.Form)Page
.FindControl("mobileForm"))
.Title);
Run Code Online (Sandbox Code Playgroud)
我猜测调用AppSettings.Get("Url")是返回null因为找不到值或调用Page.FindControl("mobileForm")返回null因为找不到控件.
您可以轻松地将其分解为多个语句来解决问题:
var configUrl = ConfigurationManager.AppSettings.Get("Url");
var mobileFormControl = Page.FindControl("mobileForm")
as System.Web.UI.MobileControls.Form;
if(configUrl != null && mobileFormControl != null)
{
img1.ImageUrl = configUrl.Replace("###", randomString) + mobileControl.Title;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107064 次 |
| 最近记录: |