Object reference not set to an instance of an object

BumbleBee picture BumbleBee · Mar 4, 2011 · Viewed 15.6k times · Source

When I try to open the page from my IDE in VS 2008 using "VIEW IN BROWSER" option I get "Object reference not set to an instance of an object" error.

The piece of code I get this error :

 XResult = Request.QueryString["res"];    
 TextBox1.Text = XResult.ToString();

Answer

JaredPar picture JaredPar · Mar 4, 2011

The problem here is that XResult is null and when you call ToString on it the code produces a NullReferenceException. You need to account for this by doing an explicit null check

TextBox1.Text = XResult == null ? String.empty : XResult.ToString();