Solved

Object reference not set to an instance of an object

Posted on 2010-09-01
7
369 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 14

Assisted Solution

by:systan
systan earned 83 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now