Error Handling (checking for null) and the GetSheetParams macro

In some previous posts I have used the following macro which will extract the value of the CADFile parameter from every sheet in your project.

public void GetSheetParams()
{
    Document doc = this.ActiveUIDocument.Document;
    string sheetInfo = "";
    foreach (Element e in new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)))
    {
        Parameter parameter = e.get_Parameter("CADFile");
        string cadFileData = parameter.AsString();
        sheetInfo += cadFileData + "\n";
    }
    TaskDialog.Show("CADFile", sheetInfo);
}

There are a couple ways this could go astray:

1) Your project has no sheets, so when you run the macro you get an empty dialog box.

emptydialog

This can be be handled as follows (new code in bold):

public void GetSheetParams()
{
    Document doc = this.ActiveUIDocument.Document;
    string sheetInfo = "";
    ICollection<Element> sheets = new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)).ToElements();
    if (sheets.Count > 0)
    {
        foreach (Element e in sheets)
        {
            Parameter parameter = e.get_Parameter("CADFile");
            string cadFileData = parameter.AsString();
            sheetInfo += cadFileData + "\n";
        }
    }
    else
    {
        sheetInfo = "There are no sheets in this project";
    }
    TaskDialog.Show("CADFile", sheetInfo);
}

2) Another problem occurs if there is a sheet but that sheet does not have the “CADFile” parameter.

exception

This happens because

e.get_Parameter("CADFile")

returns null (i.e. nothing) when the parameter “CADFile” does not exist. Then the exception is thrown on the next line

parameter.AsString()

Because you can’t call AsString() (or much of anything else) on a null variable.

This can be handled with:

public void GetSheetParams()
{
    Document doc = this.ActiveUIDocument.Document;
    string sheetInfo = "";
    ICollection<Element> sheets = new FilteredElementCollector(doc).OfClass(typeof(ViewSheet)).ToElements();
    if (sheets.Count > 0)
    {
        foreach (Element e in sheets)
        {
            string parameterName = "CADFile";
            string cadFileData = "";
            Parameter parameter = e.get_Parameter(parameterName);
            if (parameter != null)
                cadFileData = parameter.AsString();
            else
                cadFileData = "Sheet '" + e.Name + "' has no parameter named '" + parameterName + "'";
            sheetInfo += cadFileData + "\n";
        }
    }
    else
    {
        sheetInfo = "There are no sheets in this project";
    }
    TaskDialog.Show("CADFile", sheetInfo);
}

Which results in the much nicer error dialog:
noparameter

The “if” statement that checks if parameter is null is known as a “null check”. Many many software bugs are caused by programmers who forget to check for null values and programs that assume that a variable will not be null. There are different ways to handle these situations, and different people have different opinions about what is most robust, readable, etc..

Advertisements

3 thoughts on “Error Handling (checking for null) and the GetSheetParams macro

  1. I am wondering if there is a way to select elements or list their ID’s based on the Warning that they generated. For example would it be possible to select all Room Seperation lines that threw up a warning that lines are slightly off axis and may cause innacuracies? I am asking because I would then be able to select them all at once/isolate and fix as needed. This would go for a lot of other warnings that in Revit UI display element ID but I am not sure how to get to it.

    Thank you,

    Konrad

    • If the data is in the exported warnings file, you can do anything that the API supports. Selecting elements and/or listing the IDs of elements in the warnings file should be doable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s