Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Object reference not set to an instance of an object

Posted on 2010-09-01
7
Medium Priority
?
376 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 249 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 59

Assisted Solution

by:HainKurt
HainKurt earned 252 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 252 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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 
LVL 14

Assisted Solution

by:systan
systan earned 249 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 249 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 249 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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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