Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable error: "Object reference not set to an instance of ans object."! ???

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

Scheduled Pinned Locked Moved C#
helpquestioncryptography
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gman44
    wrote on last edited by
    #1

    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 }

    C H J 3 Replies Last reply
    0
    • G gman44

      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 }

      C Offline
      C Offline
      Charlie Williams
      wrote on last edited by
      #2

      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 say hash[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; }

      H 1 Reply Last reply
      0
      • G gman44

        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 }

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        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 because hash[1] is returning null, which you're trying to call ToString 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 for GetHashCode on any class). If you're trying to index the second element (remember that in .NET indexes are 0-based) of the Hashtable, 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 for NullReferenceException 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 your Hashtable. It would simply be empty.

        Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

        1 Reply Last reply
        0
        • C Charlie Williams

          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 say hash[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; }

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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). Coding hash[1] would only work if an object was added to the Hashtable using the integer (Int32, unless otherwise cast) value 1 for the key.

          Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

          C 1 Reply Last reply
          0
          • G gman44

            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 }

            J Offline
            J Offline
            Jay Shankar
            wrote on last edited by
            #5

            While adding the object in the HashTable hash you have used hash(tmp[0], tmp[1]) where tmp[0] is a string and also the key value for string object tmp[1] where as while accessing the hash value you are using hash[1], here key u r using as int (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

            1 Reply Last reply
            0
            • H Heath Stewart

              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). Coding hash[1] would only work if an object was added to the Hashtable using the integer (Int32, unless otherwise cast) value 1 for the key.

              Software Design Engineer Developer Division Sustained Engineering, Microsoft My Articles

              C Offline
              C Offline
              Charlie Williams
              wrote on last edited by
              #6

              D'oh! :doh: Charlie if(!curlies){ return; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups