Solved

Hashtable error: "Object reference not set to an instance of ans object."

Posted on 2004-08-24
6
283 Views
Last Modified: 2010-04-15
Okay, my project is almost finished and this seems to be the last bug. My error is on line 613 during the last 'while' code.  It seems that garbage collection has 'dumped' my initial 'hash' instance???   I then get the error message "Object reference not set to an instance of ans object."!

Q. How do I fix the code to eliminate the error???

try
{
while((line = reader.ReadLine()) != null)                        
{                  
string [] tmp = line.Split('\t');
if(tmp.Length == 2)
{
if(!hash.Contains(tmp[0]))
{
hash.Add(tmp[0], tmp[1]);
}                              
listBox1.Items.Add(tmp[0] + "\t" + tmp[1]);
}
}
}
finally
{
reader.Close();
}



while(myReader2.Read())
{
if (! hash.ContainsKey(str_col_DestIP.Trim()))
{
string strDestDns = "Use Other...";
}
else
{      
string str = (hash[1].ToString());     // line 613
}







0
Comment
Question by:kvnsdr
  • 3
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
ID: 11888396
string str =  hash[str_col_DestIP.Trim()];

0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11888587
you can't just get out the first index, since you do'nt know that it exists....

the hash set could very well contain no keys or values, so hash[1] is not set to an object and that is why you are getting the error.  do what gergory said and index it by what you just found to be true.  
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11888924
I really appreciate all your patience. I hope I can code as well as you guys so I can return the favor... thank you very much!!!

Before I start my next question I would like to commet on the first part that you've already answered. I thought about using the "string str =  hash[str_col_DestIP.Trim()];", however some how I kept thinking about the hash[1] as the data. Well anyway it does make sense now that jjackson explained it too. Thanks again.....

I generally write very small programs and simply link all my code in one large mass. However, I'm trying to 'professionalize' my code. So, my next question concerns sharing the data in this method with other methods........

Q. How can I write this method correctly to 'pass' the hash data to another method???

private void LoadHashtable(object obj_hash)          // this first line is the problem.........
{
try
{
while((line = reader.ReadLine()) != null)                    
{              
string [] tmp = line.Split('\t');
if(tmp.Length == 2)
{
if(!hash.Contains(tmp[0]))
{
hash.Add(tmp[0], tmp[1]);
}                        
listBox1.Items.Add(tmp[0] + "\t" + tmp[1]);
}
}
}
finally
{
reader.Close();
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:jjacksn
ID: 11889331
kvnsdr, this should go in another question perhaps.  

But, I don't really understand what you are asking...

If you want to get what is stuck into the hash table back after the end of the method, change it to:

private string LoadHashtable(object obj_hash)          // this first line is the problem.........
{
string [] tmp = null;
try
{
while((line = reader.ReadLine()) != null)                    
{              
tmp = line.Split('\t');
if(tmp.Length == 2)
{
if(!hash.Contains(tmp[0]))
{
hash.Add(tmp[0], tmp[1]);
}                        
listBox1.Items.Add(tmp[0] + "\t" + tmp[1]);
}
}
}
finally
{
reader.Close();
}
return tmp[1];
}

I'm not sure why you are using try/finally without a catch here, either.  

If this is not what you meant, please rephrase.

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11892142
the try catch finally is because there is file io jjacksn the finally is closing the file.
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11894507
I was wondering why there is no "Catch", the finally makes sense.  
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question