Solved

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

Posted on 2004-08-24
6
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…

739 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