locked
Object reference not set to an instance of an object. RRS feed

  • Question

  • User317615702 posted

    Hallo guys,

    I'm newbie in asp.net and i need your help to resolve my problem. Actually its not my program. My boss give me a program that the other person who create this program. And obviously there are bug in this program. when my boss delete all item that input before in the page , add button has lost and not appear in the page. I have checked the source code. And I think its like before. There are no changes. So my main question is why it can be happen? and how to resolve it?

    this is the error message after i change ID=iBtnAdd to ID=btnAdd

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace:
    
    
    [NullReferenceException: Object reference not set to an instance of an object.]
       backend_Bank.loadData(String type) +845
       backend_Bank.Page_Load(Object sender, EventArgs e) +167
       System.Web.UI.Control.OnLoad(EventArgs e) +91
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
    
    

    Your answer is very appreciate. thanks.

    Friday, October 24, 2014 11:14 PM

Answers

  • User281315223 posted

    Do you have any code relating to this? This could be a common issue if you have any instances of the FindControl() method within your code and you are expliciting targeting a string value for the ID of your particular element as seen below :

    // Example use of the FindControl() method
    Button addButton = FindControl("iBtnAdd") as Button;

    You would need to ensure that any previous "stringified" references like the one above are found within your code and updated to target the appropriate ID instead. Generally, whenever you are using the FindControl() method, it can be a safe bet to perform null-checking prior to any logic to avoid null reference issues :

    // Example use of the FindControl() method
    Button addButton = FindControl("iBtnAdd") as Button;
    
    // Ensure the button exists and was found
    if(addButton != null)
    {
         // Your code here
    }

    Any additional code that you might be able to provide would be extremely helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 25, 2014 12:19 PM

All replies

  • User541108374 posted

    Hi,

    this is the error message after i change ID=iBtnAdd to ID=btnAdd

    Where did you change it? In the page? Also check if the .designer page got updated as well. As we can't see what's the code actually giving the exception it's a bit hard but perhaps you tried manually to change the click event or something?

    If you go back to your previous state, so before changing the name of the button does it work again? I strongly suggest you make use of a system to keep track of changes in your code like TFS, Visualstudioonline, Git(hub.com), ...

    Grz, Kris.

    Saturday, October 25, 2014 11:49 AM
  • User281315223 posted

    Do you have any code relating to this? This could be a common issue if you have any instances of the FindControl() method within your code and you are expliciting targeting a string value for the ID of your particular element as seen below :

    // Example use of the FindControl() method
    Button addButton = FindControl("iBtnAdd") as Button;

    You would need to ensure that any previous "stringified" references like the one above are found within your code and updated to target the appropriate ID instead. Generally, whenever you are using the FindControl() method, it can be a safe bet to perform null-checking prior to any logic to avoid null reference issues :

    // Example use of the FindControl() method
    Button addButton = FindControl("iBtnAdd") as Button;
    
    // Ensure the button exists and was found
    if(addButton != null)
    {
         // Your code here
    }

    Any additional code that you might be able to provide would be extremely helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 25, 2014 12:19 PM