0

Ok - I am a little confused. I have a line of code that I have put in a Try.Catch - and it is giving me the following message:

Object reference not set to an instance of an object.

The line of code is :

            MainFormRef.xtraTabControl_Results.TabPages.Add(xtraTabPage_Results);

These are DevExpress controls.

Here I am adding the TabPage that I create at the beginning of the routine - to the TabControl_Results that is a control I have placed on the form. So - I know that both objects exist. I have even added code to reference them - and it does not throw an exception.
As well - this is a routine that I call from another place in the code - and it works fine. So - there must be something I am doing that is causing this error in the current code path. But I have to admit I am puzzled.
Is there a way to tell which object reference is Null? It does not seem to be either of the objects in the line of code that is causing the exception??

Any suggestions would be appreciated!

Code - that is causing the exception

    public DevExpress.XtraTab.XtraTabPage AddResultGridTabObject()
    {
        DevExpress.XtraTab.XtraTabPage xtraTabPage_Results = new DevExpress.XtraTab.XtraTabPage();

        var NewTabObject = new UserControl_ResultsTab();
        NewTabObject.TrackerId = SavedTrackerId;
        NewTabObject.FormType = TAIIDFormType.ftResultsGrid;
        TabList.Add(NewTabObject);
        xtraTabPage_Results.Controls.Add(NewTabObject);     // Add to the Collection 
        NewTabObject.Dock = DockStyle.Fill;                 // Now have it take up the entire Tab
        try
        {
            MessageBox.Show("Name: " + 
                MainFormRef.xtraTabControl_Results.TabPages.Count.ToString());
            MainFormRef.xtraTabControl_Results.TabPages.Add(xtraTabPage_Results);
        }
        catch (Exception Error)
        {
            MessageBox.Show("Error while Saved Encrypted File: " + Error.Message);
        }
        MainFormRef.xtraTabControl_Results.SelectedTabPage = xtraTabPage_Results;
        return xtraTabPage_Results;
    }
  • 5
    Normally I would step through the code with the debugger inspecting each of the variables. – ProgrammingLlama Oct 15 '19 at 02:24
  • I don't have with DevExpress, but another thought I had is that perhaps the `XtraTabPage` control requires that some property needs to be set, and that that property hasn't been set. – ProgrammingLlama Oct 15 '19 at 02:30
  • You could add checks to the code to determine what's null. – juharr Oct 15 '19 at 02:42
  • John, I will have to check with DevExpress - as every variable I check in this code - shows as being assigned... I will update this if I find out what is going on... – G Bradley MacDonald Oct 16 '19 at 18:32
  • Hello Everyone - thank you for the replies! I have been using the debugger and tracing through the code - but every object seems to be assigned properly (?) - but yet I am still getting the null reference. The line MainFormRef.xtraTabControl_Results.TabPages.Add(xtraTabPage_Results); both the objects on this line - and the "NewTabObject" are assigned and are not null. I have to admit to be puzzled... I will check with DevExpress as to what might be going on... – G Bradley MacDonald Oct 16 '19 at 18:33

1 Answers1

-1

I know of the following methods:

  1. Debug and inspect
  2. Logging
tymtam
  • 31,798
  • 8
  • 86
  • 126