Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

'System.NullReferenceException' error

Hi all,
from my last posting I found out how to iterate through a database table (thanks for that).  I seem to have done something wrong though.  The code compiles ok but when I run it I get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in customer.dll
Additional information: Object reference not set to an instance of an object.

My code is as follows:

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
            strConnection += " Data Source=D:\\projects\\db1.mdb";
            string strCommand = "SELECT * FROM Customer";
            OleDbConnection conn = new OleDbConnection(strConnection);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(strCommand, conn);

            DataSet ds = new DataSet();
            foreach (DataRow dr in ds.Tables[0].Rows)
                arrCustomerID[RowIndex]= Int32.Parse(dr["CustomerID"].ToString());
                arrCustomer_Surname[RowIndex] = dr["Cust_Surname"].ToString();
                arrCustomer_Forname[RowIndex] = dr["Cust_FirstName"].ToString();
                arrCustomer_House[RowIndex] = dr["Cust_House"].ToString();
                arrCustomer_Street[RowIndex] = dr["Cust_Street"].ToString();
                arrCustomer_Address1[RowIndex] = dr["Cust_Address1"].ToString();
                arrCustomer_Address2[RowIndex] = dr["Cust_Address2"].ToString();
                arrCustomer_Town[RowIndex] = dr["Cust_Town"].ToString();
                arrCustomer_County[RowIndex] = dr["Cust_County"].ToString();        
                arrCustomer_Postcode[RowIndex] = dr["Cust_PostCode"].ToString();
                arrCustomer_Telephone[RowIndex] = dr["Cust_Telephone"].ToString();
                arrCustomer_Discount[RowIndex] = Int32.Parse(dr["Cust_Discount"].ToString());
                arrCustomer_Cell_Phone[RowIndex] = dr["Cust_CellPhone"].ToString();
                RowCount = RowIndex;

The debugger halts the program when it gets to the foreach loop.

This is really driving me crazy.  I've tried of re-structuring the code but nothing works.  Please help, I'm running out of time!

shaun bethell
Thursday, April 29, 2004

Exactly which line of code?

Duncan Smart
Thursday, April 29, 2004

Also  -why are you copying all the data into arrays? DataSet/DataTables are a disconnected storage anyway - you can do this:

DataSet ds = new DataSet();
conn.Close(); // note that our work is done with connection now

// Do stuff with dataset...

Duncan Smart
Thursday, April 29, 2004

The application that I am developing is really just a vehicle for something else.  It was a poor choose of vehicle really but I'm running out of time so i'm afraid i'm stuck with it.  I have to do it in c# and I'd never used it until almost three weeks ago.  So my code is a little poor to say the least.

Basically, I have an application that calls a controller class.  This controller class instantiates two versions of the same component (in this case the customer classes).  The idea is that when a component is upgraded, the new version and old version can be run along side each other.  If the new version messes up, the controller class automatically switches to the original version. 

Only the result from calls to one of these components is passed to the client (as far as the client is concerned there is only one component).  However, the results from both components are sent to a secondary application (via a TCP socket connection) for analysis.  The Controller class does some other runtime stuff such as execution timing, dynamically switching off of one of the components or adding a new version.

So whereas I would normally have created an instance of the component calss for each row of the table, I haven't got time to redesign the whole thing to accommidate such changes.  So as it stands I can only have one instance of each of the customer components

I was kind of hopeing there was a DataSet.MoveNext type method but I couldn't find one.  If I was using the DataSet directly in a windows application I could use the Position method but i couldn't find a way in a standard class.


I have figured out the problem and I would tell you what it was but if there was a competion for being the worlds biggist idiot, you would vote for me :-(

What the heck... I'll tell all but don't laugh.
You I have two windows applications in the same solution (along with the components and component interfaces).  One is the client and one is the monitoring app.  The monitoring app was set to the default start up  project.  So when I right clicked on the client project and started a new instance I got the error.  Worse still, I did the same thing last week but forgot about it.

Thanks for replying.  I really appreciate it.


shaun bethell
Thursday, April 29, 2004

The Fill function returns the number of rows it filled the DataTable with; what number are you getting?

(And out of curiosity, is there a reason you're not using strongly-typed DataSets?)

Thursday, April 29, 2004


Thursday, April 29, 2004

i'm getting 3 (it's just a test database).  I believe using strongly typed DataSets is slower?  Too be honest, I've just about changed everything when I was trying to sort the error out.

shaun bethell
Thursday, April 29, 2004

Figured out how to get rid of the arrays too. Or someone else did anyway.  They sent me this.

ds.Tables[0].Rows is already an array and you access the 198 th row like :
if  (198 < ds.Tables[0].Rows.Count)
    DataRow dr = ds.Tables[0].Rows[198]
Now, once you have a DataRow you already have an array.
You access any value in two ways ; by index and by column name:
string Surname = dr["Cust_Surname"].ToString();
string Surname = dr[1].ToString(); if the Surname is the 2nd column in the record.
string Phone  = dr[12].ToString(); for the Cust_cellPhone

To get the value of the Cust_Street column in the 198 record you can write:
string Street = ds.Tables[0].Rows[198]["Cust_Street"].Tostring();

shaun bethell
Thursday, April 29, 2004

Well... it's not an array actually. It just looks and smells like one because it has a "this[int index]" property.

Duncan Smart
Friday, April 30, 2004

*  Recent Topics

*  Fog Creek Home