---
title: "Object Reference is not set to an instance of an object error in my code"  
description: "Object Reference is not set to an instance of an object error in my code"  
author: "Anonymous User"  
published: 2013-11-14  
updated: 2013-11-14  
canonical: https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Object Reference is not set to an instance of an object error in my code

```
protected void ImageButton_enable_Click(object sender,ImageClickEventArgs e)        {       
foreach(GridViewRow gvrow in GridView_enable.Rows)        {            CheckBoxchk1=(CheckBox) gvrow.FindControl("CheckBox_select");            if(chk1.Checked == true)            {                Label lblEmail = (Label)gvrow.FindControl("Label1");                string email = lblEmail.Text;            }         }               }
```

What is [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) in my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)? i [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) this [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) "Object [Reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) is not set to an [instance of an object](https://www.mindstick.com/forum/376/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object)".

## Replies

### Reply by Anonymous User

Hi Tanuj,

You should do like this:

```
protected void ImageButton_enable_Click(object sender,ImageClickEventArgs e){   
foreach(GridViewRow gvrow in GridView_enable.Rows)    {        CheckBox chk1=gvrow.FindControl("CheckBox_select") as CheckBox;        if (chk1 !=null && chk1.Checked == true)        {            Label lblEmail = gvrow.FindControl("Label1") as Label;            if(lblEmail != null)                
Console.WriteLine(lblEmail.Text);        }     }}
```


---

Original Source: https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
