[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

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++;
                        }
0
sticar
Asked:
sticar
  • 4
  • 3
  • 2
  • +4
2 Solutions
 
jaymoFizzleCommented:
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.


0
 
sticarAuthor Commented:
redim?  Isn't that an old VB command?
0
 
checooCommented:
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
sticarAuthor Commented:
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[] ?
0
 
checooCommented:
then ensure that the calling function has instantiated the arrays before passing the same to the function
0
 
doobdaveCommented:
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
0
 
checooCommented:
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
0
 
Bob LearnedCommented:
C# does not have the concept, like VB.NET does, to redimension an existing array.  The choice is to use an ArrayList, instead of a string array, and use the ArrayList.Add method to add new elements.

Bob
0
 
sticarAuthor Commented:
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.
0
 
b1xml2Commented:
as the learned said, you use ArrayList, but I must mention here that you do not have to change much of your code.

ArrayList descriptions = new ArrayList();
ArrayList ids = new ArrayList();
cmd.CommandText = s;
cmd.Connection = conn;
conn.Open();

dtaReader = cmd.ExecuteReader();

while (dtaReader.Read())
{
      descriptions.Add(dtaReader["s_desc"].ToString());
      ids.Add(dtaReader["s_search"].ToString());
}

string [] linkDesc = (string [])descriptions.ToArray(typeof(string));
string [] linkId = (string [])ids.ToArray(typeof(string));
0
 
b1xml2Commented:
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!
0
 
RobertRFreemanCommented:
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.
0
 
sticarAuthor Commented:
I had to do it with an ArrayList like TheLearnedOne and b1XML2 suggested.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now