0

I am getting the following error:

Object reference not set to an instance of an object

I don't know what is the cause of this error and how to solve it, here is my code:

while(dr.Read())
    {
     string variant = dr.GetString(0);
     int size = dr.GetInt32(1);
     int quantity = dr.GetInt32(2);

     DataRow x = dt.Rows
                  .Cast<DataRow>()
                  .Where(r => r["variant_name"].Equals(variant) && r["size"].Equals(size))
                  .FirstOrDefault();
                  x["quantity"] = quantity;             

      }

I'm getting the error on this line -> x["quantity"] = quantity; I'm wondering why it would give a null value because I checked in my database that it should return exactly one match.

2
  • 2
    Pretty straight forward...The error is very descriptive Commented May 27, 2013 at 12:20
  • 2
    x is null, so your Where clause is not yielding any results. Commented May 27, 2013 at 12:21

3 Answers 3

3

FirstOrDefault returns null if the element is not found and you try to access this object.

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

3 Comments

Sir I already check my database and it should give at least one match.
Try adding a breakpoint just before x["quantity"] = quantity; and check the value of x. I am pretty sure it is null when you get this error.
In visual studio, go to the line you want to break at and press F9
2

On the last line x is null, fix with

if(x != null)
   x["quantity"] = quantity;

This happens if your condition (.Where) doesn't yield a match.
At that point the FirstOrDefault return the Default part
and this is a null for a reference object as explained in MSDN

The default value for reference and nullable types is null.

5 Comments

I check my database and it should give at least one match.
Sorry Sir...I don't know what to change in that line of code. What do you think is wrong in that line .Where?
No, I was thinking about a reference problem, but this is not the case. Are you sure that the string in your database field "variant_name" is written in the exact case of your test condition (no spaces or linefeed also)
If I understand you correctly, Yes I'm pretty sure that it is the same. Actually I've tried to check what is the value of the variant by using a messagebox and it returns the same.
I have no explanation then, if you have a row with the exact variant_name and exact size this code should return a match
0
while(dr.Read())
 {
  string variant = dr.GetString(0);
  int size = dr.GetInt32(1);
  int quantity = dr.GetInt32(2);

  DataRow x = dt.Rows
                .Cast<DataRow>()
                .Where(r => (r["variant_name"].ToString().Equals(variant) && Convert.ToInt32(r["size"]).Equals(size)))
                .FirstOrDefault();

   if (x != null)
   {
       x["quantity"] = quantity;  
   }       
 }

Thank you for your answer it helped me a lot. I just changed the line .Where a little bit and now it works for me. Thank you again!

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.