开发者

runtime error in running asp.net application

Am running asp.net application with access database using gridview application..while running i got the run time error as

Object reference not set to an instance of an object.

Line 41:  开发者_C百科      RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
Line 42:DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
Line 43:SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
Line 44:SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
Line 45:         }

I got this error specially in line 43..


So rblGender.SelectedValue or rblGender is null...


Perhaps the problen is with rblGender

Make assignment as follows:

RadioButtonList rblGender = GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit") as RadioButtonList;

And then check for nullability:

if (rblGender == null)
{
    //show error
}


RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].TemplateControl.FindControl("rbGenderEdit");

if it is in template field.

it can't find rbGenderEdit.


When you have a runtime error like this one, you should use your debugger to actually see what is going on behind the scene. Just put your breakpoint at the line 43 for instance, run your program in debug mode and start investigate to see what object has a null reference and try to fix it.

For instance take a look at the line 41, rblGender might be null...

EDIT You must check if the objects that you are manipulating are null, this is part of the defensive programming techniques.

In your example, as others have said you can do it this way:

if(rblGender == null) {
  // If you are running your program with a console
  // Otherwise you should display this anywhere you can or in a log file.
  Console.WriteLine("rblGender is null");
}

else if(rblGender.SelectedValue == null) {
  Console.WriteLine("rblGender.SelectedValue is null");
}

Run your program and check what is being written! This is not going to solve your problem, but it is just going to tell you where your null reference is which will help you figure out what should be fixed!

But as I said earlier you can also debug your program properly by putting a breakpoint (you know the red ball when you click on the side of the window) at the line 43, and then run your program in debug mode! When the runtime error will fireup you will be able to check whether rblGender or rblGender.SelectedValue is null.

Also, from a more general point of view, checking your object against null references will prevent your application from crashing suddenly by managing the case where an objet might have a null reference at any given time. For instance you can say:

if(my_object is null)
{
   myValue = "default";
}

else 
{
  myValue = my_objet.getValue();
}

This is just an example it could be done in a better way, using exceptions (try/catch/finally) for instance but the general idea is: check against null references!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜