1

I keep getting

Object reference not set to an instance of an object

exceptions when searching the DataTable even though I believe I have initialized the datatable.

I am trying to do a case insensitive search of the data table filtered for currentqueryvariable.

When I looked at the different data tables through a breakpoint right before the if/else, all the datasets and datatable were fully populated.

Thanks for any help.

DataSet ds = MyData;
DataTable dt = ds.Tables[tableName];
DataTable filtered = new DataTable();
filtered = dt;

if (currentqueryvariable != "")
{
    IEnumerable<DataRow> rows = 
        filtered.AsEnumerable().Where(r => r.Field<string>("question").ToUpper().Contains(currentqueryvariable.ToUpper()) 
        || r.Field<string>("options").ToUpper().Contains(currentqueryvariable.ToUpper()) 
        || r.Field<string>("explanation").ToUpper().Contains(currentqueryvariable.ToUpper()) 
        || r.Field<string>("Source").ToUpper().Contains(currentqueryvariable.ToUpper()));

    filtered = rows.CopyToDataTable();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3600400
  • 183
  • 1
  • 2
  • 10
  • Are you sure your column names does exists and never returns null? – Complexity Jul 04 '14 at 08:41
  • 2
    **WHERE** does this exception occur? Once you've found the spot - check **WHAT** is null and why. Work around this - fix the bug if it's a bug, add a check for `!= null` if that's the only way around it..... – marc_s Jul 04 '14 at 08:42
  • have you checked the currentqueryvariable value? currentqueryvariable.ToUpper() could throw exception if the variable is null. – Yuliam Chandra Jul 04 '14 at 08:44
  • @user3600400 I don't really like this new auto close on gold badge feature, but null ref questions are almost like forum-based debugging sessions. Can you please review the linked question and see if it offers any tips? My guess is one of your method calls to `Field` or something is returning null, but you immediately try to use a method on the return value. If your question departs from the duplicate, I will gladly vote to re-open, so don't take this as a dismissive gesture on my part. – Adam Houldsworth Jul 04 '14 at 08:45
  • Just a few notes: `currentqueryvariable` might be null, you are only guarding against empty string; you initialise `fitlered` to a new data table then immediately assign it a value from somewhere else; you are generally assuming that return values from method calls are not null and are immediately trying to use methods on them, this can cause null reference exceptions. – Adam Houldsworth Jul 04 '14 at 08:49
  • 1
    Thanks for the comments, I just realized I made a silly mistake - turns out that one of the columns had null values for some rows. I've updated Access to require data in the column. – user3600400 Jul 04 '14 at 08:54
  • @user3600400 Though it completely depends on debugging. One thing that you can try is to check that if .Fields() is not returning null. If that is so then, calling the ToUpper() will throw this exception – Piyush Jul 04 '14 at 09:02

0 Answers0