I'm getting an object reference not set to an instance of an object
error in the condition of a while loop and I don't know what it's happening. The code is:
while (ClassName.StaticDataTable == null || ClassName.StaticDataTable.Rows == null)
I tried to make the object names explanatory. I thought ||
was a short circuit operator so that if the first condition was true it wouldn't evaluate the second one and that the error shouldn't be coming from there. Any ideas on what is causing the error?
If you're getting null reference
exception on
while (ClassName.StaticDataTable == null || ClassName.StaticDataTable.Rows == null)
I think of for example:
ClassName == null
So may be (just guess) for you would be enough to add one ||
condition more in front of others already present, like
while (ClassName == null || ClassName.StaticDataTable == null || ClassName.StaticDataTable.Rows == null)
EDIT
There could be also a case if StaticDataTable
property not just returns a value but does something inside it which throws null reference exception.
Try to dig into that property.
Hope this helps.
Is ClassName the object receiving the 'object reference not set to an instance of an object' or the StaticDataTable?
If it is the ClassName object that is null the attempt to retrieve StaticDataTable would raise that error.
I missed the OR (||) -- the answer below should work. What appears to be happening is the first condition is NOT met when NOT NULL and therefore does NOT short circuit the second condition. While the '&& !=' below meets the '!=' and short circuits the second condition.
If you need the null then try
while (true) {
if (classname.tbl != null && classname.tbl.object != null){
break;
}
<whatever code you need>
...
} end while
This will loop until either are null (both not null) }
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.