Solved

Object reference not set to an instance of an object

Posted on 2010-09-01
7
374 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 56

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
Industry Leaders: 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

636 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