Object reference not set to an instance of an object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasanna111
    New Member
    • Apr 2010
    • 5

    #1

    Object reference not set to an instance of an object.

    Object reference not set to an instance of an object.

    hello iam getting this Error Just created the controls as an null and how to create the object for reference

    1) as my analysis i got the information that i need to create the object reference with new keyword but how to do it script using c#

    Code:
       public TextBox txtEventDate = null;
       public Calendar calEventDate = null;
       public Label lblResult = null;
       
      protected void calEventDate_SelectionChanged (Object sender,EventArgs e)
       {
          this.txtEventDate = this.FindControl("txtEventDate") as TextBox;
          this.calEventDate = this.FindControl("calEventDate") as Calendar;
          txtEventDate.Text = calEventDate.SelectedDate.ToString ("d");    
       }
    
      protected void btnSubmit_Click(Object Sender,EventArgs e)
      {
        this.lblResult = this.FindControl("lblResult") as Label;
        lblResult.Text = "You Picked" + txtEventDate.Text;  
      }
    </script>
    Last edited by tlhintoq; Apr 30 '10, 02:14 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • 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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      TIP: When you first created your question you were asked to wrap your code with [code] tags.
      [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
      It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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.


        Example:
        Code:
        Graphics g = Graphics.FromImage(m_Undo);
        Presumes that m_Undo must be good (not null)(actually exists)(not in use)(you have permissions)(do esn't time out when accessed). If that assumption fails so does the program, because you can't make anything from a file if the file is null. Get used to validating data and assumptions in your code if you want it to be robust. For example:
        Code:
        if (m_Undo != null)
        {
           bool bSuccess = false;
           // Do your thing here, for example:
           if (myObject != null) bSuccess = true;
           // or
           if (denominator > 0) bSuccess = true;
           // or
           if (MyFunctionReturn != Failed) bSuccess = true;
           // Hurray, your thing worked!
        
           if (bSuccess)
           {
              // Then do this other thing if it worked
           }
           else
           {
              // Then do the failure recovery part / user failure message
           }
        
           return bSuccess; // If you want the calling method to know if it worked.
        }

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm not entirely sure what you are doing.

          Look how you are declaring your controls at the top of the code you posted...notice how you're setting them to "null"?

          Notice how you are trying to access them later?
          If you try to do something with an object that is set to "null" then you will get the error youre seeing

          You should probably add the Textbox, Calendar, and Label controls in your ASPX page (declaritively) instead of doing what you're doing. Remove the declarations from the top...add the controls by dragging them and dropping them onto the page instead (easiest way to do it).

          -Frinny

          Comment

          • prasanna111
            New Member
            • Apr 2010
            • 5

            #6
            Thanks For the Reply

            Iam new to byte as well to the Dot Net .

            Thanks for suggestion

            Comment

            Working...