Solved

Object reference not set to an instance of an object

Posted on 2010-09-01
7
372 Views
Last Modified: 2013-12-17
My code below keeps on getting an error about obejct instance. The strance thing is that I call this procedure in a Web Version of my application and it works fine no issues, but when i call the same code in my Win Form of the code It fails.....

It specifically fails on the line     myNewRow = lv_DataTable.NewRow();
the lv_DataTable.NewRow is null when its first run, since I am trying to populate the grid based on my Dictionary



        public void PopulateDataTable
            (Dictionary<string, string> lv_StockCheckGridCols
            , Dictionary<string, string> lv_ColumnFormatGridViewDictionary
            , DataTable lv_DataTable
            , string lv_DbENV)
        {
            try
            {
                // this is to populate the data table and bind to the datagrid
                DataRow myNewRow;
                myNewRow = lv_DataTable.NewRow();
                Decimal TempValueCurrency;
                DateTime TempValueDate;

                //and populate the data
                foreach (KeyValuePair<string, string> StockCheckPair in lv_StockCheckGridCols)
                {
                    if (StockCheckPair.Value != "")
                    {
                        switch (lv_ColumnFormatGridViewDictionary[StockCheckPair.Key])
                        {
                            case "N":
                                {
                                    TempValueCurrency = Convert.ToDecimal(StockCheckPair.Value);
                                    myNewRow[StockCheckPair.Key] = TempValueCurrency.ToString("c");
                                    break;
                                }
                            case "D":
                                {
                                    //TempValueDate = Convert.ToDateTime(StockCheckPair.Value);
                                    //myNewRow[StockCheckPair.Key] = TempValueDate.ToString("c");
                                    myNewRow[StockCheckPair.Key] = StockCheckPair.Value;
                                    break;
                                }

                            case "S":
                                {
                                    myNewRow[StockCheckPair.Key] = StockCheckPair.Value;
                                    break;
                                }
                            default:
                                {
                                    myNewRow[StockCheckPair.Key] = StockCheckPair.Value;
                                    break;
                                }
                        }
                    }
                    else
                    {
                        myNewRow[StockCheckPair.Key] = StockCheckPair.Value;
                    }
                }
                //add the row to the table
                lv_DataTable.Rows.Add(myNewRow);
            }
            catch (Exception err)
            {
                Console.WriteLine("<b>Message:</b> " + err.Message
                                  + "<br /><br />"
                                  + "<b>Source:</b> " + err.Source
                                  + "<br /><br />"
                                  + "<b>Stack Trace:</b> " + err.StackTrace);
            }

        }//public void PopulateDataTable    
0
Comment
Question by:jhacharya
7 Comments
 
LVL 3

Assisted Solution

by:omnimaven
omnimaven earned 83 total points
ID: 33583231
I would put a break point at that line, run it in debug mode and make sure that what you are passing in for  lv_DataTable is not NULL.
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 84 total points
ID: 33583240
looks like first time you are passing nothing for lv_DataTable. If it is nothing, then NewRow method cannot create the row since it will be based on columns...
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 84 total points
ID: 33583272
No instance of lv_DataTable is created and you are trying to use its method as lv_DataTable.NewRow();  So pass the instance of DataTable  lv_DataTable in your PopulateDataTable function.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 14

Assisted Solution

by:systan
systan earned 83 total points
ID: 33583571
Do like that;
            DataTable lv_DataTable = new DataTable();
            ...
            ...
            DataRow myNewRow;
            myNewRow = lv_DataTable.NewRow();
0
 
LVL 6

Accepted Solution

by:
nipunu earned 83 total points
ID: 33583858
Try,

if( lv_DataTable == null )
{
    lv_DataTable = new DataTable();
    DataRow myNewRow;
    myNewRow = lv_DataTable.NewRow();
}
else
{
    DataRow myNewRow;
    myNewRow = lv_DataTable.NewRow();
}
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 83 total points
ID: 33584090
DataTable.NewRow() is guaranteed to return a new Row match the schema of the table. This means the problem is not that there's not Row created but there are high chances that the reference to the table itself is null.

Debug.Assert(lv_DataTable != null, "DataTable refernce is null");

as your first statement in your code.
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 33588897
FRom a novie programmer this was initially hard to understand what people where telling me, but as I started to understand the solution started to make sense
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question