Solved

Object reference not set to an instance of an object

Posted on 2010-09-01
7
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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