locked
Object reference not sent to an instance of an object. Can't find the source

    Question

  • Is there a way to find these types of bugs quickly? I don't know what could be causing it but I know where it might be occuring. I've debugged line by line about 2 thousand lines of code over 5 times and don't see anything. This is my case.

    I have a search window that has a gridview of names. When someone double clicks on a name it opens a new window with all their profile information. That same profile information window is used for data entry and has 2 constructors. A profile loaded constructor which fills all the data into it for reading and an empty one for data entry. After opening with the loaded constructor the program crashes after about a second but not when its loaded using the blank constructor. It crashes at

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace Crime_Information_System
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmLogOn()); //<<crashes here
            }
        }
    }


    And just in case my stack trace helps I will paste it. I see telerik windows mouse event args in there but I have not done anything with that so I'm not sure what it could be. 

       at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.UI.RadGridView.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at Telerik.WinControls.RadControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Crime_Information_System.Program.Main() in C:\Users\msavage\Documents\Visual Studio 2010\Projects\Crime Information System\Crime Information System\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

    Monday, June 25, 2012 9:21 PM

Answers

  • I did try it but it didn't solve the issue. Telerik responded and told me that it was a bug on their end and have released an internal build to fix it. They said it was some problem that was happening when their gridview was being disposed. 
    Wednesday, July 11, 2012 10:17 PM

All replies

  • It seems strange that the stack trace does not include anything about your form that is created.  Can you provide the constructor code for frmLogOn?

    --
    Mike
    Monday, June 25, 2012 11:21 PM
  • It appears that something in your constructor is crashing, an instance is not created, causing an initialized variable to be passed to app.Run.  There are only a handful of things that can cause an app to suddenly crash, and the fact you get a message when it crashes speaks volumes. 

    An error message means that it can probably be caught in a try/catch block.  Have you tried putting one in your c'tor?  The delay makes my gut start to grumble with the flavor of something overflowing, which can take time to overflow.  If it was getting past the c'tor, then the crash would probably be reported in the class itself.  Your app does not appear to be getting that far. 

    My gut says that the c'tor itself is crashing, and not returning a reference to an instance.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Monday, June 25, 2012 11:40 PM
    Moderator
  • It seems strange that the stack trace does not include anything about your form that is created.  Can you provide the constructor code for frmLogOn?

    --
    Mike

    One more detail I forgot to mention. When someone double clicks a name in my grid view it uses a PK for that row and pulls all their info from the database. After this it launches the profileview/data entry screen and fills it. The final step is that the search screen with the grid view closes itself using this.close(); Curiously I commented out that section of code and left the window open upon successful load of a record. When I did that the program did not crash and I was able to close it manually using the "x" on the top right corner of the window. It almost seems as if the window closes and somewhere in code there is an object trying to be initialized or used that used to be on that form. Thats the only thing I can think of but can't find it and its strange because I have not messed with that particular section of code.

    Heres the code you requested. 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using System.Data.SqlClient;
    
    namespace Crime_Information_System
    {
        public partial class frmLogOn : Telerik.WinControls.UI.RadForm
        {
            //instantiating a new instance of my logoncredentials struct from my logon class
            
            SqlConnection launch = new SqlConnection();
            int intTimer = 0;
            int[] intCount = new int[19];
            //set up timer for some reason, I dont even use this but ill leave it here...
            
            public frmLogOn()
            {
                InitializeComponent();
                launch = ConnectionString.getConnection(); //gets connection string from my class/method
            }
            
            private void radButton2_Click(object sender, EventArgs e)
            {
                Cursor.Current = Cursors.WaitCursor;
                LogOn.LogInCredentials test = new LogOn.LogInCredentials();
                test.strVillageCode = txtVillageCode.Text;
                test.strUserName = txtUserName.Text;
                test.strPassWord = txtPassWord.Text;
                //sqlInjectionPrevention.scanText(this, test);
    
                string[] strTextBoxArray = new string[3] { txtVillageCode.Text, txtUserName.Text, txtPassWord.Text };
                //textbox array that will hold the 3 boxes and their values that the user has inputted
                Int64 intVillageID = 0;
    
                timer1.Interval = 1000;
                timer1.Start();
    
    
                intVillageID = LogOn.getVillageID(strTextBoxArray);//pretty obvious, calls my method to 
                //retrieve the village ID based on the village code the user inputs
                
    
                if (intVillageID != 0)
                {   //gets the password using my logon class and method
                    string strPassWord = LogOn.getPassWord(strTextBoxArray);
          
    
                    if (strPassWord == txtPassWord.Text)
                    {   //instantiating a new instance of my logoncredentials struct from my logon class
                        LogOn.LogInCredentials logOn;
                        logOn =LogOn.getUserAndVillageInfo(strTextBoxArray);
                        //^filling struct instance with user and village info using method/class
                        //Below variables are being set up to be passed to another method that checks
                        //to see if the user or village has any outstanding violations to justify a lockout
                        Int64 intUserPK = logOn.intUserID;
                        Int64 intVillagePK = logOn.intVillageID;
    
                        int intLockOutStatus = LockOutCheck.locked(intVillagePK, intUserPK);
    
    
                        if (intLockOutStatus == 0)
                        {
                            int UserIndex = 0;
    
                            
                            frmSplashScreen splash = new frmSplashScreen(logOn);
                            splash.Show();
                            this.Hide();
                            Cursor.Current = Cursors.Default;
                        }
    
                        if (intLockOutStatus == 2)
                        {
                        }
    
                        if (intLockOutStatus == 4)
                        {
                        }
                    }
    
                    else
                    {
                        MessageBox.Show("The log in criteria you have specified is not correct. Please try again.", "Incorrect"
                        + " Credentials");
                        Cursor.Current = Cursors.Default;
                    }
                }
    
                else
                {
                    MessageBox.Show("The log in criteria you have specified is not correct. Please try again.", "Incorrect"
                        + " Credentials");
                    Cursor.Current = Cursors.Default;
                }
    
    
                
            }
    
            private void frmLogOn_Load(object sender, EventArgs e)
            {
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
    
    
    
            }
    
            private void frmLogOn_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                    btnLogOn.PerformClick();
            }
    
            private void txtPassWord_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                    btnLogOn.PerformClick();
            }
    
            private void radButton1_Click(object sender, EventArgs e)
            {
                //string[] strTextBoxArray = new string[3] { txtVillageCode.Text, txtUserName.Text, txtPassWord.Text };
                LogOn.LogInCredentials logOn = new LogOn.LogInCredentials();
                
    
    
                frmSplashScreen splash = new frmSplashScreen(logOn);
                splash.Show();
                this.Hide();
            }
        }
    }
    

    Now that I look back at this I realize it's really messy and unprofessional. I wrote this almost a year ago lol.

    Monday, June 25, 2012 11:42 PM
  • I would put a try/catch arround the "launch = ConnectionString.getConnection();"  It seems you have an error there.  Pop up a message regarding the error if it occurs.

    --
    Mike
    Tuesday, June 26, 2012 12:06 AM
  • One more detail I forgot to mention. When someone double clicks a name in my grid view it uses a PK for that row and pulls all their info from the database. After this it launches the profileview/data entry screen and fills it. The final step is that the search screen with the grid view closes itself using this.close(); Curiously I commented out that section of code and left the window open upon successful load of a record. When I did that the program did not crash and I was able to close it manually using the "x" on the top right corner of the window. It almost seems as if the window closes and somewhere in code there is an object trying to be initialized or used that used to be on that form. Thats the only thing I can think of but can't find it and its strange because I have not messed with that particular section of code.


    That quacks like a duck.   I think it's a duck.  I forgot to mention the "comment code until it stops" approach.  Good move.  Check out any event handlers dynamically subscribing to objects declaread and initialized in other classes.  You need to unsubscribe to those events when the form closes.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 26, 2012 1:20 AM
    Moderator

  • That quacks like a duck.   I think it's a duck.  I forgot to mention the "comment code until it stops" approach.  Good move.  Check out any event handlers dynamically subscribing to objects declaread and initialized in other classes.  You need to unsubscribe to those events when the form closes.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    I have spent all day so far tracking it down. So far I have not found anything. It is pretty ridiculous. I have no idea what it could be. I opened an older backed up version of my program from 3 days ago and it does not crash like the newer version. So what I did was copy and paste all the code on the first form of my old version to my first form on my new version. Still same problem. Every time I comment out the this.close() its all good and no problems occur; however, I do not like to band-aid bugs. 
    Tuesday, June 26, 2012 7:20 PM
  • I messed around with the debugging options and got a new error

    ((System.ComponentModel.ISupportInitialize)(this.cIS_DEVDataSet)).EndInit();

    ^Auto Code

    System.ArgumentOutOfRangeException crossed a native/managed boundary
      Message=Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.
    Parameter name: Value
      Source=System.Windows.Forms
      ParamName=Value
      StackTrace:
           at System.Windows.Forms.DateTimePicker.set_Value(DateTime value)
      InnerException: 

    Tuesday, June 26, 2012 7:27 PM
  • ...and your form is not referenced by other classes?

    ...and your form is not passed objects instantiated in other classes?

    ...and you checked every event handler on your form, to be sure that they only handle events from object declared and instantiated in your form?

    ...and you checked for any dynamically instantiated delegates and objects on your form that get passed to other classes?  What else?  Hmm?

    Three days ago.  If "this.Close()" makes it stop, then that is telling.  Something is still referencing the form.  Or something is referencing an object or variable declared on your form, which is now closed and disposed.  Or an event handler callback is calling your form, and it is now disposed.  There are not too many scenarios to cause it too happen.  The hard part is finding it when it is not obvious.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 26, 2012 7:38 PM
    Moderator
  • It appears that something in your constructor is crashing, an instance is not created, causing an initialized variable to be passed to app.Run.  There are only a handful of things that can cause an app to suddenly crash, and the fact you get a message when it crashes speaks volumes. 

    An error message means that it can probably be caught in a try/catch block.  Have you tried putting one in your c'tor?  The delay makes my gut start to grumble with the flavor of something overflowing, which can take time to overflow.  If it was getting past the c'tor, then the crash would probably be reported in the class itself.  Your app does not appear to be getting that far. 

    My gut says that the c'tor itself is crashing, and not returning a reference to an instance.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Rudedog I just saw this.. I try and caught my constructor and my form load event. Nothing.. Still crashing at the main entry point of the app. Over flow seems to make sense only I can't think of what explicitly could be causing it. I decided to go deeper into trouble shooting. I got an old copy of the program out from 3 days ago, copy and pasted all the .cs code from the old datagridview form into the new form and did the same with the data entry screen code. I still get the error in the newer version where I do not get it in the older version. It makes me think the problem does not lie with in those two forms but somewhere else. I've checked my event handlers I have a pretty complex program and I don't see anything that sticks out. The only thing I've recently done is add a button to the main tool bar of the parent MDI form that launches a new window not part of the MDI to function as an emailing center screen. Offcourse with in that form theres alot going on to retrieve emails etc etc. One more thing I added was a call as soon as the program is opened to a database to pull directory info. It is on a separate thread and that same thread runs through 3 methods. It pulls the directory info in 2 methods and then pulls info that shows wether or not they have new emails. 
    Tuesday, June 26, 2012 7:46 PM
  • have you tried anything like the following....

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
                Application.Run(new Form1());
            }
    
    
            static void MyHandler(object sender, UnhandledExceptionEventArgs args)
            {
                Exception e = (Exception)args.ExceptionObject;
                Console.WriteLine("MyHandler caught : " + e.Message);  // write to log/text file
                Console.WriteLine("Inner Exception : " + e.InnerException);  //write to a log/text file;'
            }

    If I add that to a WinForms app, the it catches the following exception.  The UnhandledException handler reports the actual exception, Invalid Operation.

            private void button2_Click(object sender, EventArgs e)
            {
                throw new InvalidOperationException("Invalid Operation Exception");
            }

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    Tuesday, June 26, 2012 8:21 PM
    Moderator
  • I tried it. I got the same thing. Object reference not set to an instance of an object and the inner exception was empty. After the message box popped up my code still broke and crashed. 
    Tuesday, June 26, 2012 8:41 PM
  • Update, whatever it is it is either on the datagrid view form or somewhere else that is referencing that form. The data entry form has nothing to do with the problem. I knocked it out by commenting it out and the error still occured. 
    Tuesday, June 26, 2012 9:03 PM
  • I tried it. I got the same thing. Object reference not set to an instance of an object and the inner exception was empty. After the message box popped up my code still broke and crashed. 
    Don't use MessagBoxes.  Write to disk.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 26, 2012 10:17 PM
    Moderator
  • I tried it. I got the same thing. Object reference not set to an instance of an object and the inner exception was empty. After the message box popped up my code still broke and crashed. 

    You need to subscribe to the appdomain exception prior to calling Application.Run.  Do it on the first form that starts the application.  If you're getting an exception message, then you should be able to catch the exception.  Do not run/test with the Debugger, either.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 26, 2012 10:20 PM
    Moderator
  • You mentioned a DGV control.  (we have see many, many posts about issues with Telerik controls, BTW)

    How much  stuff are you doing in your Load event?  Not all controls are ready for some actions to be performed because they may not have a Handle assigned to them yet.  The Form.Shown event guarantees that all controls have Handles.


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 26, 2012 11:52 PM
    Moderator
  • Rudedog2 it is apparently an issue with Telerik. I opened an older project with an older version of telerik controls, updated the controls and immediately had the same issue. I contacted telerik and they said they knew about the issue. Supposedly an update is due by the end of this week. 
    Wednesday, July 04, 2012 6:38 PM
  • Rudedog2 it is apparently an issue with Telerik. I opened an older project with an older version of telerik controls, updated the controls and immediately had the same issue. I contacted telerik and they said they knew about the issue. Supposedly an update is due by the end of this week. 

    Have tried my suggestion regarding moving code from Form.Load to Form.Shown.  I think the basic issues surround the possibility that the controls do not yet have Handles.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Thursday, July 05, 2012 9:43 PM
    Moderator
  • I did try it but it didn't solve the issue. Telerik responded and told me that it was a bug on their end and have released an internal build to fix it. They said it was some problem that was happening when their gridview was being disposed. 
    Wednesday, July 11, 2012 10:17 PM