Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi,

I've made an login page in asp.net, where a user logins and redirected to the page according to his permissions. Now, instead to write the permission code / verify the user's permission on each and every page, I wrote the code in the class named userpermission class. But when i try to run my web application, its showing me an error

"Object reference not set to an instance of an object."

Following is the code written in class


VB
Public Class userpermissions : Inherits System.Web.UI.Page

Public Sub nwperm()

Dim mymenu As Menu = Master.FindControl("NavigationMenu")  'Here I'm getting error

Dim mymi As MenuItem = mymenu.FindItem("TechnicalSupport")

mymi.Enabled =True

Dim mymenu2 As Menu = Master.FindControl("NavigationMenu")

Dim mymi2 As MenuItem = mymenu2.FindItem("Administrator")

mymi2.Enabled =True

Dim mymenuitem As MenuItem = mymi2.ChildItems(5)

mymenuitem.Enabled =True

End Sub



If i write the same code on each and every page of my application then its running fine, Idont know whats wrong in my class.

Code written in my login page:

VB
myTool.nwperm()
(Where myTool is declared as "Public myTool As New userpermissions")



Plz help me as I'm stuck here for last 2 days.

Thanks
Posted
Comments
Rajeev Jayaram 6-Feb-12 2:53am    
This is a NullReferenceException. You are trying to use a reference variable who’s value is Nothing/null. Try yourself first to figure out where the value is null and why? Use break points/quick watch.
mirzamujib 6-Feb-12 2:58am    
Thanks Rajeev..
Yeah you are right its a NullReferenceException. But how can i solve this issue.
If same code put on each page then its working fine.

Thanks again.

Try this,

Dim mymenu As Menu = CType(Master.FindControl("YourContentPlaceHolderNameHere").FindControl("NavigationMenu"), Menu)
 
Share this answer
 
Comments
mirzamujib 6-Feb-12 2:30am    
Thanks Rajeev,

I tried but still same error on the same line.
Rajeev Jayaram 6-Feb-12 2:47am    
Did you put your ContentPlaceHolder ID here, "YourContentPlaceHolderNameHere"?
mirzamujib 6-Feb-12 2:56am    
yeah I put the contentplaceholder name as you showed above, but still same error.
Before my navigation menu was not inside the content place holder as its present on my master page. But I put my navigation menu on ContentPlaceHolder and tried and got the same error.
Rajeev Jayaram 6-Feb-12 3:04am    
You have to use your ContentPlaceHolder ID and not this "YourContentPlaceHolderNameHere".
mirzamujib 6-Feb-12 3:23am    
ok i'll try and let u know.
thanks
Try this
C#
Master.FindControl("YourContentPlaceholderId").FindControl("NavigationMenu");


Hope this will help you
 
Share this answer
 
Comments
mirzamujib 6-Feb-12 2:30am    
Thanks Salman,

I tried but same error on the same line.
Step 1

Put a quickwatch on '
VB
Master.FindControl("YourContentPlaceholderId")
' and check if its null

Step 2

Then put a check on '
VB
Master.FindControl("YourContentPlaceholderId").FindControl("NavigationMenu");
' and cehck if it is also null.
 
Share this answer
 
v2
Comments
mirzamujib 6-Feb-12 5:02am    
Thanks for the reply...
I checked its showing null, but dont understand why its executing if i write the same code on each page of my asp.net.
bbirajdar 6-Feb-12 10:42am    
Oh.. Got the issue.. You are inheriting the class from the Page class and this page class DOES NOT inherit from the Master. And the NavigationMenu is placed in the Master.

This is the reason.
bbirajdar 6-Feb-12 10:43am    
try using,

this.Parent.Master.FindControl("YourContentPlaceholderId").FindControl("NavigationMenu");

and this.Parent.Parent.Master.FindControl("YourContentPlaceholderId").FindControl("NavigationMenu");
bbirajdar 6-Feb-12 10:45am    
The best solution for you will be to write the above code in the Load event of master page instead of inherited basepage.
mirzamujib 7-Feb-12 2:51am    
thanks Birajdar..
I'll try and let u know...
thanks again...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900