Hashtable error: "Object reference not set to an instance of ans object."! ???
-
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 }
-
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 }
It would seem that your
Hashtable
(I'm assuming that's what 'hash' is) does not have an object stored at the index you're trying to retrieve. Did you mean to sayhash[0].ToString()
? Or possibly whatever you're reading in the first while loop contains only one line. These are only guesses, as not enough code is posted for a definitive answer. The garbage collector won't collect something you still hold a reference to. It's not the hash that's null, though, so that wouldn't be it anyway. Charlie if(!curlies){ return; } -
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 }
No, your
hash
variable is fine (you should really use a debugger and examine the state of variables in your Autos or Locals window). It's becausehash[1]
is returningnull
, which you're trying to callToString
on. Unrolling it, it would look something like this:object o = hash[1]; // Returns null
o.ToString();If you added "1" to your
Hashtable
, then you need to pass "1" - not 1 - to your indexer. They are very different (see the documentation forGetHashCode
on any class). If you're trying to index the second element (remember that in .NET indexes are 0-based) of theHashtable
, you're out of luck. Hash tables don't sort their collections - they use hash keys to store and reference data. You should do a search on google or something for basic theory on hash tables to find out more. There's never a good reason forNullReferenceException
to be thrown, either. When there's a chance that a variable may be null, check it. Even if you could access items like a collection, assuming that's there 2 - or even 1 - element is a bogus assumption. What if the file was empty and you didn't collect any data? There would be nothing in yourHashtable
. It would simply be empty.Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
-
It would seem that your
Hashtable
(I'm assuming that's what 'hash' is) does not have an object stored at the index you're trying to retrieve. Did you mean to sayhash[0].ToString()
? Or possibly whatever you're reading in the first while loop contains only one line. These are only guesses, as not enough code is posted for a definitive answer. The garbage collector won't collect something you still hold a reference to. It's not the hash that's null, though, so that wouldn't be it anyway. Charlie if(!curlies){ return; }There wouldn't be an item at any index: a
Hashtable
is not a collection or a list (but can expose them through properties and methods). Codinghash[1]
would only work if an object was added to theHashtable
using the integer (Int32
, unless otherwise cast) value 1 for the key.Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
-
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 }
While adding the object in the
HashTable
hash you have usedhash(tmp[0], tmp[1])
where tmp[0] is astring
and also thekey
value forstring
object
tmp[1] where as while accessing the hash value you are using hash[1], herekey
u r using asint
(value 1). This is the cause of the error. Replace the line 613 as below.if(hash.ContainsKey("1"))
{
//where "1" is the key
string str = hash["1"].ToString();}
Regards, Jay
-
There wouldn't be an item at any index: a
Hashtable
is not a collection or a list (but can expose them through properties and methods). Codinghash[1]
would only work if an object was added to theHashtable
using the integer (Int32
, unless otherwise cast) value 1 for the key.Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles
D'oh! :doh: Charlie if(!curlies){ return; }