none
Correct way to check for null objects?

    Question

  • Hi all,
    I'm using the Precedents object of an Excel Range:

    Excel.Range range = (Excel.Range)Globals.ThisAddIn.Application.ActiveCell;
    foreach (Excel.Range r in range.Precedents)
                    {
                        //stuff
                    }

    When there are precedents, this is fine.  However if there are no precedents, I get a COMException.  I'd like to do a check to prevent the exception, but "normal" checks for null/empty objects don't work, such as:
    if (range.Precedents == null)
    if (range.Precedents.Count > 0)

    These throw the same COMException.  It seems you can't "touch" a null/empty object at all.

    Placing everything inside a try/catch works, but this feels like the wrong way to be coding, is there a better option?
    Thanks...

    Thursday, October 15, 2009 9:40 PM

Answers

  • Hello Jonathan,

    If the range has not precedents, it will throw an exception when accessing this property. Here, it is right way to use try/catch block. Code like this:
                try
                {               
                    foreach (Excel.Range r in range.Precedents)
                    {
                        ......
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

    If you have any further question, please feel free to let us know.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Jonathan S. _ Monday, October 19, 2009 7:48 PM
    Monday, October 19, 2009 5:21 AM

All replies

  • Are you checking to make sure range is not null as well?

    William Wegerson (www.OmegaCoder.Com)
    Thursday, October 15, 2009 10:33 PM
  • The range is not null, even in the case of a new worksheet/blank cell.

     if (range != null)
                {
                  //this executes
                }
    Thursday, October 15, 2009 11:24 PM
  • Hello Jonathan,

    If the range has not precedents, it will throw an exception when accessing this property. Here, it is right way to use try/catch block. Code like this:
                try
                {               
                    foreach (Excel.Range r in range.Precedents)
                    {
                        ......
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

    If you have any further question, please feel free to let us know.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Jonathan S. _ Monday, October 19, 2009 7:48 PM
    Monday, October 19, 2009 5:21 AM
  • This is the fastest option, and you can check that the error message returned is "No cells were found."

    There is a much slower way of doing this, and that is to
     - ShowPrecendents
     - NavigateArrows

    cheers
    Monday, October 19, 2009 8:01 AM
  • Hi all,
    Thanks for the replies.  I've implemented the try/catch(s) and they work fine.  Was hoping for something that made the code "cleaner", but if that's the way it is, that's the way it is...
    -Jon
    Monday, October 19, 2009 7:51 PM