Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone rectify this error:

Error:

it shows error: Object reference not set to an instance of an object.

Error coding;



C#
public void totalprice()
    {
        int qty = 0;
        float price = 0;
        float rowtotal = 0;
        float subtotal = 0;
        float totalprice = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            price = float.Parse(row.Cells[4].Text);//error
            qty = Convert.ToInt32(row.Cells[3].Text);//error
           rowtotal = qty * price;
            subtotal += rowtotal;
        }
        totalprice = subtotal;
        lbltprice.Text = totalprice.ToString();
    }
}
Posted
Updated 1-Dec-11 22:33pm
v3
Comments
Reiss 2-Dec-11 4:02am    
What are the values in the cells before you try and parse/convert them?
[no name] 2-Dec-11 4:05am    
EDIT: removed "pre" tag, added "b" tag
Amir Mahfoozi 2-Dec-11 4:07am    
put a break point at that line and run to there. then observe the values of row.Cells[4].Text or row.Cells[3].Text. most of the time having null values cause this error.
M.Narmatha 2-Dec-11 4:20am    
if i get null values means what to do...
Prince Antony G 2-Dec-11 5:22am    
Will u post ur gridview coding part.Please..

C#
price = float.Parse(row.Cells[4].Text);//error

make like this
C#
price = float.Parse((((Label)row.findControl("lblName")).text))

and
C#
qty = Convert.ToInt32(row.Cells[3].Text);//error

make like this
C#
qty = Convert.ToInt32((((Label)row.findControl("lblName")).text))
 
Share this answer
 
v2
Comments
[no name] 2-Dec-11 4:07am    
use Float.TryParse method instead. This is safer than casting or converting
M.Narmatha 2-Dec-11 4:13am    
it shows error: Object reference not set to an instance of an object.
[no name] 2-Dec-11 4:16am    
this means that you are getting null value
M.Narmatha 2-Dec-11 4:20am    
then wht to do..
S.P.Tiwari 2-Dec-11 4:48am    
check wether you are given controlID is proper or not.
Use TryParse instead of Parse. In the case of TryParse, you will not get an error if your value cannot be converted into a float type.
 
Share this answer
 
Comments
[no name] 2-Dec-11 4:16am    
my 5!
Abhinav S 3-Dec-11 5:09am    
Thank you.
Monjurul Habib 2-Dec-11 23:48pm    
nice advice , my 5!
Abhinav S 3-Dec-11 5:09am    
Thank you!
one solution is to convert these two lines :

C#
price = float.Parse(row.Cells[4].Text);//error
qty = Convert.ToInt32(row.Cells[3].Text);//error


to these ones :

C#
if((row.Cells.Count>=5) && (row.Cells[4]!=null) && (row.Cells[3]!=null)) 
{
            if (!float.TryParse(row.Cells[4].Text, out price ))
                price  = 0;
            if (!float.TryParse(row.Cells[3].Text, out qty ))
                qty = 0;
}


And another way is to embrace the whole procedure in try catch block.


Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900