Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When Running Application I got this Exception: Object reference not set to an instance of an object. on Below code.
C#
fromPageRange = Functions.ToInt32(((Label)grdFiles.Rows[i].FindControl("lblFromPageRange")).Text);
Posted
Updated 19-Jul-11 23:55pm
v2

It could be that grdFiles.Rows[i].FindControl("lblFromPageRange")) does not exist. On Visual Studio, you can set a breakpoint before this line, and then run the app. When it stops on the breakpoint, add grdFiles.Rows[i].FindControl("lblFromPageRange")) to the watch and try to see if its not null.
 
Share this answer
 
Comments
rajjosh 20-Jul-11 6:07am    
it gives null value in watch
walterhevedeich 20-Jul-11 10:22am    
You found the problem then. You need to check why its returning null, or probably, you might not want to assign it to fromPageRange if its null.
your FindControl does not find the control which result in NULL.

So:
Label aLabel = (Label)grdFiles.Rows[i].FindControl("lblFromPageRange"))
if (null != aLabel && null != aLabel.Text)
{
Int32 value = Convert.ToInt32(aLabel.Text);
}
 
Share this answer
 
It might helps,

C#
public int ParseText(int positionInArray, string whichLabelToFind)
{
    Label aLabel = positionInArray <= grdFiles.Rows.Count ? (Label)grdFiles.Rows[positionInArray].FindControl(whichLabelToFind) : null;
    int result = default(int);
    return aLabel != null ? Int32.TryParse(aLabel.Text, out result) ? result : result : result;
}


:)
 
Share this answer
 
You could try the following.

bool isValid = int32.TryParse( (Label)grdFiles.Rows[i].FindControl("lblFromPageRange")), out fromPageRange );


This will not throw but you must handle if isValid = false case.
 
Share this answer
 
v2

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