[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ASP.NET VB.NET System.NullReferenceException: Object reference not set to an instance of an object

Posted on 2014-02-22
Medium Priority
Last Modified: 2014-02-22
Most probably the oldest issue here and there are loads of help but I can't seem to find an answer to my situation so hopefully someone can cast an eye over this and help.

I have a form on a page that when it loads will only show certain panels depending upon the results of the questions being asked. All was working perfectly fine until I dropped this into a LoginView and now the code doesn't work.

I assume this is due to how the controls are referenced now that they are 'hidden' behind the relevant templates but I can't seem to get the panels to reference correctly so hopefully someone can help.

Here is my page:

Code Behind:
The problem I'm having is at:
Dim vw_pnlYesContact As Panel = VolView.FindControl("pnlYesContact")
            Dim v_bitContactMade As RadioButtonList = TryCast(VolView.FindControl("bitContactMade"), RadioButtonList)

            If v_bitContactMade Is Nothing Then
                vw_pnlYesContact.Visible = False
                If v_bitContactMade.SelectedItem Is "1" Then
                    vw_pnlYesContact.Visible = True
                    vw_pnlYesContact.Visible = False
                End If
            End If

Open in new window

I'm trying to determine if the radionbuttonlist (bitContactmade) is 1 or 0 - if 1 show the panel and if 0 then don't.

On page load the panel visibility should be false (that's what I believe I'm doing here) but I keep getting a System.NullReferenceException: Object reference not set to an instance of an object error every time and it's driving me mad lol.

I assumed I was checking for this in the code above but obviously my little brain isn't comprehending what it needs to so any advice as always guys is appreciated.

Question by:Steven O'Neill
  • 3
  • 3
LVL 40
ID: 39879303
What line is triggering the exception?

Author Comment

by:Steven O'Neill
ID: 39879354
Hi there, the last code snippet above is taken from the code behind and is where the error happens.

I'm attempting to have the panel's visibility altered (so it's shown or not shown) and the first part of the last snippet above when it checks the visible element of the panel I'm getting the null error but I assumed my code was checking this but obviously I'm missing something. In that snippet it's shown as line 5.

Sorry for the vagueness here but typing this on an iPhone isn't easy as I can't capture the code snippets to show you exactly the line causing the problem.
LVL 40
ID: 39879398
If the FindControl method in line 1 does not find a panel called "pnlYesContact", it will return Nothing and you will get the NullReferenceException.

There does not seem to be a panel by that name in your page.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Steven O'Neill
ID: 39879561
That's what I am assuming myself but the panel is there in my code so I'm confused by that. The page checks if the user is authenticated and if they are then they get what's in the Loggedintemplate area and this panel is in there so I'm confused why I get the error.

As I say, brain is fried with this one lol
LVL 29

Accepted Solution

Paul Jackson earned 1600 total points
ID: 39879581
The panel control is child of the wizard, try :

Dim vw_pnlYesContact As Panel = TryCast(Me.AC_Submission.FindControl("wizStep1_Contact").FindControl("pnlYesContact"),Panel)
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 39879583
OK, I see it now. Missed if first time.

I am more of a Windows programmer than a Web programmer, but from what I understand
pnlYesContact is defined inside of wizStep1_Contact, so it will not be found when you search in VolView as you do with VolView.FindControl. You need to search for it in wizStep1_Contact.

Author Closing Comment

by:Steven O'Neill
ID: 39879787
Thanx guys, the code from Paul was really useful as put me very much onto the right direction. Thanx also to James for your assistance...you are correct in what I needed to do but the code showed me exactly how to do this and I've been able to get this sorted.

Never knew it had to be done that way...learn something new everyday lol

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question