-4

Possible Duplicate:
What is a NullReferenceException in .NET?

Object reference not set to an instance of an object.

protected void Page_Load(object sender, EventArgs e)
{
   int Role = Convert.ToInt32(Request.QueryString["Role"].ToString());
   try
   {
       if (Role != 3)
       {
           gv_ViewApplicants.Visible = true;
           gv_ViewApplicants_SelectedIndexChanged(this, new EventArgs());
       }
       else
       {
           gv_ViewApplicants.Visible = false;
       }
    }
    catch (NullReferenceException e1)
    { 

    }

 }
Community
  • 1
  • 1
user1667769
  • 13
  • 1
  • 3
  • 2
    .Net C# - here is you reply. Your "question" seem to be missing actual question part... only request to "reply .net C#". – Alexei Levenkov Sep 13 '12 at 07:08
  • Where exactly does this error occur? – Spontifixus Sep 13 '12 at 07:09
  • 3
    stop asking for the stack, stop helping within this scenario - the op should put some effort into his/her issue and follow the magic "What is a NullReferenceException in .NET?"-topic ... –  Sep 13 '12 at 07:10

4 Answers4

2

Try

int Role = Convert.ToInt32(Request.QueryString["Role"] != null ?
                           Request.QueryString["Role"].ToString() : 
                           "0");

instead of

int Role = Convert.ToInt32(Request.QueryString["Role"].ToString());

You need to check for null if not passed query string.

ssube
  • 47,010
  • 7
  • 103
  • 140
Pankaj
  • 675
  • 4
  • 3
1

The code is trying to access a member of a reference type variable that is set to null.

Please make source Request.QueryString["Role"] is not null.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
William Wu
  • 11
  • 2
  • 2
    all you do is guessing ... without a stack you can't give a qualified answer. the truth is: it could also be `gv_ViewApplicants` or something in `gv_ViewApplicants_SelectedIndexChanged`-method which is null ... –  Sep 13 '12 at 07:14
1

first thing

int Role = Convert.ToInt32(Request.QueryString["Role"].ToString());

This statement is outside try so if it crashes on QueryString being null or even if Convert.ToInt32 method throw exception, catch block will not be executed.

You can try this code

  int number;
  bool result = Int32.TryParse(Request.QueryString["Role"], out number);
  if (result)
  {
    // your implemntation       
  }
  else
  {        
    // your implemntation   
  } 

You can even use Convert.ToString(Request.QueryString["Role"]) if still u are getting this error.

Pushpendra
  • 814
  • 1
  • 6
  • 17
0

You should Never catch NullReferenceException.

However, the problem seems to be in very first line: (the only line outside try block)

int Role = Convert.ToInt32(Request.QueryString["Role"].ToString()); 

either Request is null OR QueryString["Role"] is returning null.

Share the stack trace for more clear answer.

Azodious
  • 13,752
  • 1
  • 36
  • 71
  • 1
    as the op does not provide a stack, it could also be the catch-scope he is dealing with ... so `gv_ViewApplicants` could also be null, or something in the `gv_ViewApplicants_SelectedIndexChanged`-method –  Sep 13 '12 at 07:16
  • if that was the case, wouldn't it be caught without any notification? – Azodious Sep 13 '12 at 07:21
  • the scenario is very unclear. the op could have set a breakpoint in the catch-scope. it's not clear if the exception bubbles to the ui or not ... so ... anything could be ... –  Sep 13 '12 at 07:24