Solved

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

Posted on 2004-08-24
6
269 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now