Solved

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

Posted on 2004-08-24
6
275 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Json POSt to Rest API 4 60
SQL Login 17 50
ASP.NET 5 Templates 2 78
updating order of the rows, N+1 query issue, C#, asp.net, MVC, SQL Server 3 25
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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