Simple array question

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

......

while (Reader.Read())
{

for (int i = 0; i < Reader.FieldCount; i++)
{
Argon = Convert.ToDouble(Reader.GetValue(0));
SF6 = Convert.ToDouble(Reader.GetValue(1));
C4H10 = Convert.ToDouble(Reader.GetValue(2));
R134A = Convert.ToDouble(Reader.GetValue(3));


}
}

I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));


I know this is simple, but I'm bounsing around with different
languages all the time.

Thanks
mike
 
AMP said:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

.....

while (Reader.Read())
{

for (int i = 0; i < Reader.FieldCount; i++)
{
Argon = Convert.ToDouble(Reader.GetValue(0));
SF6 = Convert.ToDouble(Reader.GetValue(1));
C4H10 = Convert.ToDouble(Reader.GetValue(2));
R134A = Convert.ToDouble(Reader.GetValue(3));


}
}

I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));

I know this is simple, but I'm bounsing around with different
languages all the time.


You have not allocated the arrays.

Since you most likely don't know the number of rows in the database,
then I suggest using List<double> instead of double[].

Arne
 
Arne said:
AMP said:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

.....

while (Reader.Read())
{

for (int i = 0; i < Reader.FieldCount; i++)
{
Argon = Convert.ToDouble(Reader.GetValue(0));
SF6 = Convert.ToDouble(Reader.GetValue(1));
C4H10 = Convert.ToDouble(Reader.GetValue(2));
R134A = Convert.ToDouble(Reader.GetValue(3));


}
}

I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));

I know this is simple, but I'm bounsing around with different
languages all the time.


You have not allocated the arrays.

Since you most likely don't know the number of rows in the database,
then I suggest using List<double> instead of double[].


And consider avoiding the Convert.ToDouble since it says "I really
don't know what data type is in the database, but I want a double".
Casts are more type safe.

Arne
 
You need to initialize the array with the proper size first.
e.g.,
in the declaration:
double[] Argon = new double[Reader.FieldCount];
or after:
Argon = new double[Reader.FieldCount];
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
You need to initialize the array with the proper size first.
e.g.,
in the declaration:
double[] Argon = new double[Reader.FieldCount];
or after:
Argon = new double[Reader.FieldCount];
--
David Antonhttp://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB



AMP said:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

 while (Reader.Read())
            {
                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    Argon = Convert.ToDouble(Reader.GetValue(0));
                    SF6 = Convert.ToDouble(Reader.GetValue(1));
                    C4H10 = Convert.ToDouble(Reader.GetValue(2));
                    R134A = Convert.ToDouble(Reader.GetValue(3));

                }
            }
I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));

I know this is simple, but I'm bounsing around with different
languages all the time.
Thanks
mike- Hide quoted text -

- Show quoted text -


David, Its not the fields we are populating with, its the values.
How do we get around not knowing how many items there are going to
be???...Besides a LIST<>
 
As Arne suggested, "List<>" is indeed the recommended way to go here then.

--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB


AMP said:
You need to initialize the array with the proper size first.
e.g.,
in the declaration:
double[] Argon = new double[Reader.FieldCount];
or after:
Argon = new double[Reader.FieldCount];
--
David Antonhttp://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB



AMP said:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

while (Reader.Read())
{
for (int i = 0; i < Reader.FieldCount; i++)
{
Argon = Convert.ToDouble(Reader.GetValue(0));
SF6 = Convert.ToDouble(Reader.GetValue(1));
C4H10 = Convert.ToDouble(Reader.GetValue(2));
R134A = Convert.ToDouble(Reader.GetValue(3));

I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));

I know this is simple, but I'm bounsing around with different
languages all the time.
Thanks
mike- Hide quoted text -

- Show quoted text -


David, Its not the fields we are populating with, its the values.
How do we get around not knowing how many items there are going to
be???...Besides a LIST<>
 
As Arne suggested, "List<>" is indeed the recommended way to go here then..

--
David Antonhttp://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB



AMP said:
You need to initialize the array with the proper size first.
e.g.,
in the declaration:
double[] Argon = new double[Reader.FieldCount];
or after:
Argon = new double[Reader.FieldCount];
--
David Antonhttp://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;
......
 while (Reader.Read())
            {
                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    Argon = Convert.ToDouble(Reader.GetValue(0));
                    SF6 = Convert.ToDouble(Reader.GetValue(1));
                    C4H10 = Convert.ToDouble(Reader.GetValue(2));
                    R134A = Convert.ToDouble(Reader.GetValue(3));
                }
            }
I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));
I know this is simple, but I'm bounsing around with different
languages all the time.
Thanks
mike- Hide quoted text -
- Show quoted text -

David, Its not the fields we are populating with, its the values.
 How do we get around not knowing how many items there are going to
be???...Besides a LIST<>- Hide quoted text -

- Show quoted text -


All,
Thanks,I'll give that a try.
Mike
 
AMP,

Use a *Select Count from xxx where xxx in an SqlCommand.ExecuteScalar to
get the count, that is made for this kind of things.

Be aware that your method does not give any advantage above using a
DataTable
Especially because you are using numbers as indexer for the columns, the
change on errors in future is high.

Cor
 
chance on errors


Cor Ligthert said:
AMP,

Use a *Select Count from xxx where xxx in an SqlCommand.ExecuteScalar to
get the count, that is made for this kind of things.

Be aware that your method does not give any advantage above using a
DataTable
Especially because you are using numbers as indexer for the columns, the
change on errors in future is high.

Cor

AMP said:
What am I doing wrong?
double[] Argon;
double[] SF6;
double[] C4H10;
double[] R134A;

.....

while (Reader.Read())
{

for (int i = 0; i < Reader.FieldCount; i++)
{
Argon = Convert.ToDouble(Reader.GetValue(0));
SF6 = Convert.ToDouble(Reader.GetValue(1));
C4H10 = Convert.ToDouble(Reader.GetValue(2));
R134A = Convert.ToDouble(Reader.GetValue(3));


}
}

I am getting Object reference not set to an instance of an object at
Argon = Convert.ToDouble(Reader.GetValue(0));


I know this is simple, but I'm bounsing around with different
languages all the time.

Thanks
mike

 
Back
Top