Link to home
Start Free TrialLog in
Avatar of sticar
sticar

asked on

DataReader help get value... ahh what am I doing wrong?

Error is : Object reference not set to an instance of an object.

string [] linkDesc;
string [] linkId;
                        cmd.CommandText = s;
                        cmd.Connection = conn;
                        conn.Open();

                        dtaReader = cmd.ExecuteReader();
                  
                        while (dtaReader.Read())
                        {
bombs on this line:                                   linkDesc[lcnt] = dtaReader["s_desc"].ToString();
                              linkId[lcnt] = dtaReader["s_search"].ToString();
                              lcnt++;
                        }
Avatar of jaymoFizzle
jaymoFizzle

My first guess is that the array doesn't have a fixed dimension.

in vb I would normally
redim LinkDesc[20]

or in your case
redim LinkDesc[dtaReader.Rows.Count]
(or something like this. Sorry don't have VS running on this computer)

Basically i think the array is not dimensioned with a length. you should be able to test the length before hand. or even assign the array element 0 a string beforehand and see if it will still crash.


Avatar of sticar

ASKER

redim?  Isn't that an old VB command?
this line string [] linkDesc

should be string [] linkDesc = new string[10];

if you dont know the what size the array should be then consider using an arraylist
Avatar of sticar

ASKER

Unfortunately, I'm trying to fix someone elses code and I don't want to have to rewrite everything.  string[] linkDesc and string[] linkId are both passed into this function.  

Can I not just get the values from the "Select *..." and store it in the string[] ?
then ensure that the calling function has instantiated the arrays before passing the same to the function
The comments posted here all point to the same problem:
One of your objects has not been instantiated.

The samlpe code does not seem complete, as you say some parameters are passed in? ("string[] linkDesc and string[] linkId are both passed into this function.")

If that is the case, and the arrays are already dimensioned, then one of your other objects needs to be instantiated, eg:
cmd = new SqlCommand ' <- or whatever command object you're using.

or the connection object?

conn = new SqlConnection

Check through yur code to ensure theses objects get instantiated.

Note, the DataReader object does NOT need to be instantiated, as it gets an instance from the cmd.ExecuteReader function.

Hope this helps,

David
its definately not the connection object or the command object because if it was connection object then conn.open will give error and if it was command object then cmd.executereader will give an error,

in all probability it should be the array thats causing the problem
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sticar

ASKER

That's what I thought TheLearnedOne.  I'll see if I can change it to an Array and not have to change too much code.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ArrayLists allows for casting of the items to an array of a specified type


for instance

ArrayList list = new ArrayList();
list.Add(1);
list.Add(4);
list.Add(200);

int [] numbers = (int [])list.ToArray(typeof(int));

will get you an array of integers...

pretty handy to know this, one would think!
Here is a link for a straightforward method of data access:
http://vsnetdatabinding.blogspot.com/

The datatable can be used to create an array or arraylist.
Avatar of sticar

ASKER

I had to do it with an ArrayList like TheLearnedOne and b1XML2 suggested.

Aparently using the "new" keyword lets .net automatically assign memory.