Simple array question

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
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
D

David Anton

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
 
A

AMP

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<>
 
D

David Anton

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<>
 
A

AMP

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
 
C

Cor Ligthert[MVP]

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
 
C

Cor Ligthert[MVP]

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

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top