GetOrderedParameters() and Parameter.AsValueString() - Object reference not set to an instance of an object

GetOrderedParameters() and Parameter.AsValueString() - Object reference not set to an instance of an object

EATREVITPOOPCAD
Collaborator Collaborator
366 Views
1 Reply
Message 1 of 2

GetOrderedParameters() and Parameter.AsValueString() - Object reference not set to an instance of an object

EATREVITPOOPCAD
Collaborator
Collaborator

I am trying to find all sheet's parameters that have a string value of 'X' and store their names and values (In this case always 'X')

 

These sheet parameters have varying names per project, such as "50% CD Set" or "Owner Progress Set" and will have a value 'X' or be empty. Our office standards don't have any other parameters with a value 'X' so this isn't an issue.

 

The problem I have is I am getting an exception on some parameters .AsValueString() that I haven't been able to skip using try{}catch{}

 

Any ideas on which parameter could be doing this and how to skip them?

 

Here is the exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Autodesk.Revit.DB.Parameter.AsValueString(...) returned null.

 

                int issuePackageParamName = 0;
                int issuePackageParamValue = 1;

                Dictionary<string, string[,]> exSheetsIssuePackages = new Dictionary<string, string[,]>();

                foreach (ViewSheet v in exCivilSheets)
                {
                    exCivilSheetsIDs.Add(v.Id);

                    // GET ISSUE PACKAGES
                    IList<Parameter> ps = v.GetOrderedParameters();
                    
                    string[,] issuePackages = new string[2, 6];

                    int packageIndex = 0;

                    foreach (Parameter p in ps)
                    {
                        try
                        {
                            if (p.AsValueString().ToLower() == "x")
                            {
                                issuePackages[issuePackageParamName, packageIndex] = p.Definition.Name;
                                issuePackages[issuePackageParamValue, packageIndex] = p.AsValueString();
                            }
                        }
                        catch { }

                        ++packageIndex;
                    }

                    exSheetsIssuePackages.Add(v.Name,issuePackages);
                }

 

And this is the problematic line of code

if (p.AsValueString().ToLower() == "x")

 

The definition of insanity is doing the same thing over and over again and expecting different results
0 Likes
Accepted solutions (1)
367 Views
1 Reply
Reply (1)
Message 2 of 2

EATREVITPOOPCAD
Collaborator
Collaborator
Accepted solution
                        if(p.HasValue)
                            if (p.AsValueString().ToLower() == "x")

 

A strange phenomenon happens sometimes after I give up and post here, a light bulb goes off....

The definition of insanity is doing the same thing over and over again and expecting different results