0
foreach (DataRow row in dttemp.Rows)
    dt_final.ImportRow(row);
foreach (DataRow row in dttemp1.Rows)
    dt_final.ImportRow(row);

i am getting this exception on the LAST line here

when i check the contents of row are NOT null

what am i doing wrong?

4
  • is it possible that dt_final or dttemp or dttemp1 are null? Commented Dec 22, 2010 at 19:53
  • Obviously one of your variables you access is not set to a valid object reference. Maybe its dt_final, maybe its somewhere in the function ImportRow. Hard to guess from here. Commented Dec 22, 2010 at 19:54
  • @everyone if dt_final were null then i would get this exception on the 2nd line, not on the last Commented Dec 22, 2010 at 19:56
  • 1
    Honestly, instead of having everyone guess, why not just set a breakpoint and run the thing in debugger? Commented Dec 22, 2010 at 20:01

2 Answers 2

2

A NullReferenceException occurs usually when you try to invoke a member or method on a null object (in other words, when you use the . operator on something that is null). My best guess is that dt_final or perhaps dttemp1 as other users have suggested is null.

Sign up to request clarification or add additional context in comments.

2 Comments

if dt_final were null then i would get this exception on the 2nd line, not on the last
Not necessarily. If dttemp.Rows is empty, the first for loop won't run. This can be a particularly tricky exception. Many times, you need to carefully trace your code step by step to track down the issue.
1

dt_final is definitely null, just because nothing else can be. The question is why it throws error only at last line. Reason is that dttemp.Rows is empty and first foreach is not executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.