Solved

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

Posted on 2004-08-24
6
289 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

685 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