As for the variable ending up null. That's because you can do this
Panel
pnl2 = (Panel)null;
Therefore the last cycle is not getting a panel. The reasons for which are not evident by your code sample.
As your panels are direct children of one Control consider removing the ID dependency and just loop through the child controls setting only the Panels like so (likely to be faster than FindControl as it hits each child only once) -
protected void Page_Load(object sender, EventArgs e)
{
HidePanels(objTabNav);
}
private void HidePanels(Control parent)
{
foreach (Control obj in parent.Controls)
{
if (obj is Panel)
{
Panel pnl = (Panel)obj;
pnl.Visible = false;
}
}
}
Rgds,
Martin.