none
NullReferenceException RRS feed

  • Question

  • I have code

    row = tables[8].Rows.Add();//This I have referense
                            cells = row.Cells;                        
                            cells[6].Range.Text = string.Format("{0:f3}", emission);
                            cells[7].Range.Text = string.Format("{0:f3}", normEmission);
                            var rows = tables[8].Rows;
                            tables[8].Cell(rows.Count, 1).Select();
                            application.Selection.MoveUp(WdUnits.wdLine, 1, WdMovementType.wdExtend);
                            application.Selection.Cells.Merge();
                            row = tables[8].Rows.Add();//BUT THIS I have null WHY????
                            row = tables[8].Rows.Add();
                            cells = row.Cells;//Exeption An unhandled exception of type 'System.NullReferenceException'

    Why I have NullReferenceException???

    Friday, April 14, 2017 2:14 PM

All replies

  • Hi Ahlompys,

    When the table has vertically merged cells, I could reproduce your issue, the row object is null. 

    To work around, I suggest you iterate through table cells and access the last row cells via it's rowindex.  Here is the example for getting  last row cells object into a list.

    List<Word.Cell> lstCell=new List<Cell>();
    foreach (Cell cell in tables[1].Range.Cells)
                {
                    if (cell.RowIndex == tables[1].Rows.Count)
                    {
                        lstCell.Add(cell);                                                     
                    }
                }

    Best Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 17, 2017 1:01 PM
    Moderator