Error: Object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srimathiparthasarathy
    New Member
    • Feb 2010
    • 18

    Error: Object reference not set to an instance of an object

    i am debugging my project,am using code behind c#.net with sql 2005.i got the error Object reference not set to an instance of an object. what shall i do?
    can anyone tell me reason?but my data can stored in table.!!!!!!!!! !
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    "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.

    I can make some general suggestions that apply to good coding practice.
    • Assume that everything is broken, or at least not ideal.
    • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
    • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
    • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
    • Check that files/folders exist, even if you just did a call to make it: You may not have permissions.
    • Don't assume the harddrive has room for what you are doing: They do fill up. Usually right in the middle of you writing to your log file.
    • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
      • Put a breakpoint on the first line of the method causing trouble.
      • When the code stops there, walk through line by line with F-10.
      • Check the values of your assumptions (looking at the Locals and Automatic variable windows as well as hovering the mouse over the variables in the code (hothelp will popup).
    • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
    • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.

    Comment

    • navyjax2
      New Member
      • Aug 2007
      • 18

      #3
      I find that whenever I get that error, it's that the code is returning something that is null to a variable, and then I'm trying to test that variable for something, like:

      Code:
      SPList myList = oWeb.GetList("\Lists\SomeList");
      SPItem oItem = myList.GetItemByID(1);
      string myFieldVal = oItem["someField"].ToString();
      if (myFieldVal == "hello world") { return true; } else { return false; }
      If the "someField" item value is null, you'd get that error instead of true or false. To correct, in my example, I do:

      Code:
      string myFieldVal = oItem["someField"] != null ? oItem["someField"].ToString() : String.Empty;

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        My first thought would be that oItem is null/nothing.
        But it could be that myList is null/nothing.
        Or it could be that there is nothing at oItem's index "someField"

        So try this:
        Code:
          SPList myList = oWeb.GetList("\Lists\SomeList");
          if(myList != null)
          {
            SPItem oItem = myList.GetItemByID(1);
            if(oItem != null)
            {
              string myFieldVal = oItem["someField"];
              if(String.IsNullOrEmpty(myFieldVal) == false)
              {
                if (myFieldVal == "hello world") 
                { return true; } 
                else { return false; }
              }else{return false;}
            }
          }
          return false;
        Put breakpoints in to discover which variable is null/nothing.
        -Frinny
        Last edited by Frinavale; Jul 26 '12, 03:09 PM.

        Comment

        • navyjax2
          New Member
          • Aug 2007
          • 18

          #5
          Oh, I was just using mine as an example and didn't put much thought into it, but you are right that any of them could be null and cause an exception, and should all be bracketed off just like you did. Thanks for making my answer better :) .

          Originally posted by Frinavale
          My first thought would be that oItem is null/nothing.
          But it could be that myList is null/nothing.
          Or it could be that there is nothing at oItem's index "someField"

          So try this:
          Code:
            SPList myList = oWeb.GetList("\Lists\SomeList");
            if(myList != null)
            {
              SPItem oItem = myList.GetItemByID(1);
              if(oItem != null)
              {
                string myFieldVal = oItem["someField"];
                if(String.IsNullOrEmpty(myFieldVal) == false)
                {
                  if (myFieldVal == "hello world") 
                  { return true; } 
                  else { return false; }
                }else{return false;}
              }
            }
            return false;
          Put breakpoints in to discover which variable is null/nothing.
          -Frinny

          Comment

          Working...