3

The 1st time I was applying bold to columns, but I only wanted a row to be bold not the whole column. So I decided to do the same thing but using row.RowStyle. Compiled without errors, but what I did get is a runtime error where is says r.RowStyle.SetFont(font);. I made a class that to do with everything related to excel and it's in this class I get this error(r.RowStyle.SetFont(font);):

NullReferenceException was handled

Object reference not set to an instance of an object.

Debugged the whole process and nothings null. I don't understand why I get this error when using RowStyle and when I use CellStyle I don't get that error.

This is my class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;   

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;

using System.IO;


//works with everything related to excel
namespace takeMyTime_text
{
    class exelSheet
    {
            string exelPath; // where it's being saved

        HSSFWorkbook wb2 = new HSSFWorkbook();

        ISheet sheet;

        IRow r;

        IFont font;

        String[] headerTitles = { "Date", "In", "Out", "In", "Out", "Description" };

        // assing values to class variables
        public void getValues(string path, string worksheetName)
        {
            exelPath = path;
        }


        //excel header
        public void header()
        {
            #region set bold properties

            font = wb2.CreateFont();

            font.FontHeightInPoints = 11;

            font.FontName = "Arial";

            font.Boldweight = (short)FontBoldWeight.Bold;

            #endregion

            sheet = wb2.CreateSheet("test sheet");

            //se tiene que usar esto cada vez que vallas a escribir en el mismo row
            r = sheet.CreateRow(0);

            r.RowStyle.SetFont(font);

            for (int i = 0; i < headerTitles.Length; i++)
            {
                r.CreateCell(i).SetCellValue(headerTitles[i]);
            }
        }

        //excel footer
        public void footer(int row, int col, string totalHours, int row2, int col2)
        {
            //ws.Cells[row, col] = new Cell("Worked hours:");

            //ws.Cells[row2, col2] = new Cell(totalHours);

            //wb.Worksheets.Add(ws);

            //wb.Save(exelPath);            
        }

        // write the date on the excel file
        public void writeDate(DateTime dt, int col, int row)
        {
            r = sheet.CreateRow(row);

            r.CreateCell(col).SetCellValue(dt.Month + "/" + dt.Day + "/" + dt.Year);          
        }

        //write and value on a cel
        public void writeValues(string text, int col, int row)
        {
            //r = sheet.CreateRow(row);

            r.CreateCell(col).SetCellValue(text);           
        }

        //guarda la info en un excel
        public void writeToFile()
        {
            FileStream file = new FileStream(exelPath, FileMode.Create);

            wb2.Write(file);

            file.Close();
        }
    }
}
Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85
user3842769
  • 95
  • 1
  • 2
  • 10
  • I forgot to mention I debugged it and it's not null. Has the bold value, font name and height in points – user3842769 Sep 19 '14 at 20:18
  • Nothing is null. Where is the null? That's what I need to know. I know how this error works I don't understand npoi works, yet. – user3842769 Sep 19 '14 at 20:29

1 Answers1

5

Looks like your RowStyle is null. Try the following:

var style = wb2.CreateCellStyle();
style.SetFont(font);

r = sheet.CreateRow(0);
r.RowStyle = style;
Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85
  • worked! but how come what I had doesn't work? – user3842769 Sep 19 '14 at 21:16
  • @user3842769 Pierre-Luc Pineault posted a great link that perfectly explains the issue. RowStyle property of your row is not initialized, therefore calling SetFont method on it throws an exception. In the example above I create a style object first and then assign it to the row's RowStyle property. – Maksim Vi. Sep 20 '14 at 00:54