An unhandled exception of type 'System.NullReferenceException'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • willemr
    New Member
    • May 2010
    • 9

    An unhandled exception of type 'System.NullReferenceException'

    Hi Everybody,

    I've been working with webservices. But I keep stumbling on the same error.

    An unhandled exception of type 'System.NullRef erenceException '
    Additional information: Object reference not set to an instance of an object.

    This is my piece of code

    Code:
      getKeyTags _keyTags = new getKeyTags();
                _keyTags.login = login;
                KeyTagBean[] keyTags = null;
                lock (wService)
                {
                    keyTags = wService.getKeyTags(_keyTags);
                }
                Hashtable keyTagHash = new Hashtable();
                foreach (KeyTagBean bean in keyTags)
                {
                   keyTagHash.Add(bean.keyTagID, bean);
                }  
                dataGridView1.DataSource = keyTagHash;
    When I get the error, the keyTagHash.Add line highlights.


    Can anyone help me please?

    THanks
    Last edited by tlhintoq; May 11 '10, 03:25 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    is bean.keyTagID is unique?

    Regards
    Dheeraj Joshi

    Comment

    • willemr
      New Member
      • May 2010
      • 9

      #3
      yes it is.

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by willemr
        Hi Everybody,

        I've been working with webservices. But I keep stumbling on the same error.

        An unhandled exception of type 'System.NullRef erenceException '
        Additional information: Object reference not set to an instance of an object.

        This is my piece of code
        getKeyTags _keyTags = new getKeyTags();
        _keyTags.login = login;
        KeyTagBean[] keyTags = null;
        lock (wService)
        {
        keyTags = wService.getKey Tags(_keyTags);
        }
        Hashtable keyTagHash = new Hashtable();
        foreach (KeyTagBean bean in keyTags)
        {
        keyTagHash.Add( bean.keyTagID, bean);
        }
        dataGridView1.D ataSource = keyTagHash;

        When I get the error, the keyTagHash.Add line highlights.


        Can anyone help me please?

        THanks
        keyTags = wService.getKey Tags(_keyTags);
        is resulting a null value to keyTags... check it out first on debug

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by OriginalPoster
          Original Poster: "How do I fix a 'object reference not set to an instance of an object' error?
          The line your code stopped on while debugging holds all your answers.
          One of the variables/objects was created but not initialized. For example:
          Code:
          string TempString;// Created but not initialized so this is still null
          //versus
          string TempString = string.empty;
          Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.

          Comment

          • willemr
            New Member
            • May 2010
            • 9

            #6
            Thanks Guys, I managed to fix it.

            Comment

            Working...