An exception of type "System.NullReferenceException" occurred in myproject.DLL, but was not processed in the user code

What does this error mean? I keep getting this error, it works fine, and it just started throwing this error .... any help?

img1.ImageUrl = ConfigurationManager.AppSettings.Get("Url").Replace("###", randomString) + Server.UrlEncode(((System.Web.UI.MobileControls.Form)Page.FindControl("mobileForm")).Title); 

An exception of type 'System.NullReferenceException' occurred in MyProject.DLL but was not processed in user code

Additional Information: The reference to the object is not installed in the instance of the object.

+6
c # mobile-website
source share
2 answers

This means that somewhere in your call chain you tried to access a Property or call a method on an object that was null .

Given your statement:

 img1.ImageUrl = ConfigurationManager .AppSettings .Get("Url") .Replace("###", randomString) + Server.UrlEncode( ((System.Web.UI.MobileControls.Form)Page .FindControl("mobileForm")) .Title); 

I assume that calling AppSettings.Get("Url") returns null because the value is not found, or calling Page.FindControl("mobileForm") returns null because the control was not found.

You can easily break this down into several statements to solve the problem:

 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; } 
+4
source share

That means you have a null link somewhere out there. Can you debug the application and stop the debugger when it gets here and investigates? Probably img1 is null or ConfigurationManager.AppSettings.Get("Url") returns null.

+2
source share

All Articles