0

Which is a better code to avoid throwing Object reference not set to an instance of an object when using Telerik Radtextbox? Are both codes below the same? Can I set a default value to avoid nullreference from throwing?

protected void btnAddSAles_click(object sender, EventArgs e)        
{  
   string orderName = Ordername.Text;
}

or

protected void btnAddSAles_click(object sender, EventArgs e)        
{    
   TextBox b = item.FindControl("Ordername") as TextBox;            
   string box1 = b.text;            
}
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yuval Itzchakov Jul 26 '14 at 12:03
  • @GrantWinney it throws an error when assigning the textbox value to a string. Is there a way to avoid throwing nullexception? Appreciate your help. Thanks – user3878949 Jul 26 '14 at 12:08
  • @YuvalItzchakov it's not the same issue. – user3878949 Jul 26 '14 at 12:09
  • @GrantWinney Object reference not set to an instance of an object. at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) – user3878949 Jul 26 '14 at 12:10

2 Answers2

0

I am assuming FindControl is returning null from the as cast you're trying to make. I assume (again) it isn't finding a control named Ordername, hence you are trying to access a Text property on a null object, which causes the NullReferenceException.

What you should do is:

  1. Check why there is no control named Ordername, as im assuming there should be one
  2. If the control which invoked the Button.Click may not always be a TextBox object, add a nullity check:

    protected void btnAddSAles_click(object sender, EventArgs e)        
    {    
       TextBox b = item.FindControl("Ordername") as TextBox;      
       if (b != null)
       {      
          string box1 = b.text;
       }           
    }
    
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • The Ordername textbox is existing on my page but then there are times wherein the nullreference error is thrown. Do you know any way to replicate this kind of issue? – user3878949 Jul 26 '14 at 12:40
0

The two code samples are not the same.

The first sample won't throw that exception, unless you explicitly set Ordername = null somewhere in your code, which I doubt you did.

The second sample will throw because you're trying to cast a RadTextBox to a TextBox (I'm assuming, since you're asking about RadTextBox), which results in b being null.

If you want to avoid the possibility of a null reference exception, then you have to check for null before accessing properties on the control:

protected void btnAddSAles_click(object sender, EventArgs e)        
{    
   TextBox b = item.FindControl("Ordername") as TextBox;  

   if (b != null)
   {
       // do something with b.Text;
   }
}

The real issue here is that you're casting to the wrong type. The RadTextBox does not derive from the native TextBox control. You have to cast to a RadTextBox.

protected void btnAddSAles_click(object sender, EventArgs e)        
{    
   RadTextBox b = item.FindControl("Ordername") as RadTextBox;  

   if (b != null)
   {
       // do something with b.Text;
   }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Thank you is string orderName = Ordername.Text same as TextBox b = item.FindControl("Ordername") as TextBox string orderName = b.Text? – user3878949 Jul 26 '14 at 12:38
  • This issue is actually intermittent. So there are times wherein nullreference throws because it could not find the radtextbox but it does not happen all the time. Do you have an idea which scenario may throw this error? Does a postback may cause this issue or something to do with sessionstate? Thank you so much for helping – user3878949 Jul 26 '14 at 12:57