revit encountered a system.nullreferenceexception object reference not set to ob

revit encountered a system.nullreferenceexception object reference not set to ob

wahedfazeli
Enthusiast Enthusiast
2,433 Views
3 Replies
Message 1 of 4

revit encountered a system.nullreferenceexception object reference not set to ob

wahedfazeli
Enthusiast
Enthusiast

i have this code for import from excel data to revit this is my code:

{

UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            //Application app = uiapp.Application;
            Document doc = uidoc.Document;
            string fileman = Path.Combine(Path.GetTempPath(), "MyExcelbad.xlsx");

            using (Transaction t = new Transaction(doc, "param"))
            {
                t.Start();
                using (ExcelPackage package = new ExcelPackage(new FileInfo(fileman)))
                {
                    ExcelWorksheet sheet = package.Workbook.Worksheets["Wall"];

foreach (WallType elem in new FilteredElementCollector(doc).OfClass(typeof(WallType)).WhereElementIsElementType())
                    {
                        //stacked don't have cost
                        if (elem.Kind == WallKind.Stacked)
                        {
                            continue;
                        }
                        for (int value = 2; value <= sheet.Dimension.Rows; value++)
                        {
                            if (sheet.Cells[value, 1].Value.Equals(elem.Name))
                            {
                                if (sheet.Cells[value, 1].Value.Equals("Exterior - Brick on Mtl. Stud"))
                                {
                                    continue;

                                }
                                if (sheet.Cells[value, 1].Value.Equals("Exterior - Block on Mtl. Stud"))
                                {
                                    continue;

                                }
                                sheet.Cells[value, 8].Value = elem.Name;
                                if (null != elem.get_Parameter(BuiltInParameter.ALL_MODEL_COST))
                                {
                                    Parameter cost = elem.get_Parameter(BuiltInParameter.ALL_MODEL_COST);
                                    cost.Set(sheet.Cells[value, 4].Value.ToString());
                                    elem.get_Parameter(BuiltInParameter.KEYNOTE_PARAM).Set(10);
                                }
                            }
                        }
                    }
                    package.Save();
                }
                    t.Commit();
            }
            return Result.Succeeded;
       }

but i have this error

i dont know why this error happened for me

error.PNG

this is line 64

:

error.PNG

0 Likes
2,434 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk

Step through your code line by line and you will see what is causing the problem.



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

wahedfazeli
Enthusiast
Enthusiast
Thanks dear jeremy
I found my problem the cells of excel were null this create problem for me
Thank you
0 Likes
Message 4 of 4

Anonymous
Not applicable

To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and reference types.

So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!

Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.

 

Reference types (these ones must be checked):

 

  • dynamic
    object
    string

 

Value types (you can simply ignore these ones):

 

  • Numeric types
    Integral types
    Floating-point types
    decimal
    bool
    User defined structs