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

Posted on 2014-02-22
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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 100 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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