0

I am trying to add a Data Table to a DataTable list (post a filter operation) that belongs to a kind of modal class. I am getting the below exception on my debug statement _return.SearchResult.Add(_i);. I am getting the same error in my actual statement as well _return.SearchResult.Add(_t);. Kindly help me to identify what is the issue.

Thanks & Regards, S. Sudharsan

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=*******
  StackTrace:
   at *******.*******.searchCMDBData(String SearchString) in D:\GIT_VS2019_WorkSpace\*******\*******\*******.asmx.cs:line 38
   at *******.*******._Default.DoSearch(Object sender, EventArgs e) in D:\GIT_VS2019_WorkSpace\*******\*******.*******\Default.aspx.cs:line 57

        public QR searchCMDBData(string SearchString)
        {
            QR _return = new QR();
            _return.responseType = ResponseType.MESSAGE;
            _return.responseMsg = "Successfully called as expected";
            string INVENTORY_FILE = ConfigurationManager.AppSettings["INVENTORY_FILE_PATH"];

            List<DataTable> _ret = new List<DataTable>();
            _ret = CMDB.GetRequestsDataFromExcel(INVENTORY_FILE);

            foreach (DataTable _i in _ret)
            {
                _return.SearchResult.Add(_i);
                if (_i.TableName == "Inventory Consolidated")
                {
                    DataTable _t = _i.AsEnumerable()
                            .Where(r => r.Field<string>("IP Address").Equals(SearchString) || r.Field<string>("Host Name").Equals(SearchString))
                            .CopyToDataTable();
                    if (_t.Rows.Count > 0)
                    {
                        _return.SearchResult.Add(_t);
                    }
                }
            }
            return _return;
        }
public enum ResponseType { SUCCESS, MESSAGE };

    public class QR
    {
        public ResponseType responseType;
        public string responseMsg;
        public List<DataTable> SearchResult;
    }
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Hint: where do you initialize SearchResult? – Klaus Gütter Nov 03 '20 at 05:27
  • _return.SearchResult. is expected to be null during the first iteration. Hence it being null is expected. But please tell me if there is any mistake in the way i have initialized the modal class "QR". – Sudharsan Simhan Nov 03 '20 at 05:33
  • 1
    You initialize `_ret` with a new List. Then, you immediately throw that away and set it to the return value of `GetRequestsDataFromExcel`. But as @klausgutter points out, your NRE comes from the call to `_return.SearchResult.Add`. Unless you have a `QR` constructor you are not showing, `SearchResult` is null. You should have been able to deduce this **_very_** quickly in the debugger (the code you show would be a good as an example to practice using the debugger) – Flydog57 Nov 03 '20 at 05:35
  • if `_return.SearchResult` is null, you can't call `Add` on it - it will throw an NRE – Flydog57 Nov 03 '20 at 05:36
  • Hook.. My bad.. I dint had understood @klausgutter 's Hint properly :-) Thanks for point to use the constructor. I understood it was not initialized. But not sure where to initialize it. Initializing it in the constructor fixed it.. Thanks for the quick help.... ! – Sudharsan Simhan Nov 03 '20 at 05:44

1 Answers1

0

You have to new up the List<DataTable> SearchResult, otherwise you are trying to insert a value not into an empty list, but into a null list.

You could do this in the constructor of the QR class like this:

public class QR
{
    public ResponseType responseType;
    public string responseMsg;
    public List<DataTable> SearchResult;

    public QR(){
      SearchResult = new List<DataTable>();
    }
}
Jonathan
  • 4,115
  • 2
  • 18
  • 35