Solved

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

Posted on 2014-02-22
7
1,034 Views
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
            Else
                If v_bitContactMade.SelectedItem Is "1" Then
                    vw_pnlYesContact.Visible = True
                Else
                    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.

Thanx
0
Comment
Question by:Steven O'Neill
  • 3
  • 3
7 Comments
 
LVL 40
ID: 39879303
What line is triggering the exception?
0
 
LVL 2

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.
0
 
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 2

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
0
 
LVL 29

Accepted Solution

by:
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)
0
 
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.
0
 
LVL 2

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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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