简体   繁体   中英

Object reference not set to an instance of an object

I'm sorry if this is a really simple one but it's really confusing me.
I'm getting:

NullReferenceException was unhandled by user code : Object reference not set to an instance of an object.

When I run the following code:

a.Forms.Add(AppraisalForm.CreateNew(int.Parse(Session["AppID"].ToString()), option, 
    owner, webStatics.AuditUser(Session)))

I've used a breakpoint and is shows that:

Session["AppID"] is 14
Option is HOY
Owner is ETJ
AuditUser(Session) is [CBX]

I'm not sure what other information is needed but would be greatful if someone could help me out.

Is the CreateNew returning null? Is a null? The easiest way to find it is to break each element out and assign it to it's own variable. That will tell you which piece is failing.

This problem is precisely why you should never write code like this.

Look at the values of a, a.Forms, AppraisalForm (if this is not a class), the return value of AppraisalForm.CreateNew, and the value of webStatics. Something in there is null. It would help to split this statement apart into multiple statements so you can see which line is causing the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM