0

I'm trying to solve an issue in my code. It's a devBooster code, so pretty similar to .NET. I have a List of objects and I'm looping over each one to populate a stream and finally export this Stream to Excel file thanks to a Template.

This is my foreach loop which populate Stream :

foreach (var resultat in ListeDesGroupesNotationsItemVM.Resultat)
{
    string codeAlgo = resultat.CodeAlgo.ToString();
    string codeSegment = resultat.CodeSegment.ToString();

    worksheet.Cells[ligne, 1].Value = resultat.Nom;
    worksheet.Cells[ligne, 2].Value = resultat.IdCMCIC;
    worksheet.Cells[ligne, 3].Value = resultat.NomGac;
    worksheet.Cells[ligne, 4].Value = resultat.Refbilancielle;
    worksheet.Cells[ligne, 5].Value = resultat.Cotation;
    worksheet.Cells[ligne, 6].Value = resultat.Secteur;
    worksheet.Cells[ligne, 7].Value = codeAlgo.IsBlank() ? "" : codeAlgo;
    worksheet.Cells[ligne, 8].Value = codeSegment.IsBlank() ? "" : codeSegment;
    worksheet.Cells[ligne, 9].Value = resultat.DateDerniereCotation;
    worksheet.Cells[ligne, 10].Value = resultat.Statut;
    worksheet.Cells[ligne, 11].Value = resultat.PolePresentateur;
    worksheet.Cells[ligne, 12].Value = resultat.Pays;

    ligne++;
}

I have an issue with two fields which are ENUM field : resultat.CodeAlgo and resultat.CodeSegment.

The error message when I try to compile is :

NullReferenceException was unhandled by user code Additional information: The object reference is not defined as an object instance (sorry for translation)

To my mind, I have to manage NULL field right ?

Thank you by advance

Arfizur Rahman
  • 384
  • 4
  • 13
Essex
  • 6,042
  • 11
  • 67
  • 139
  • *I have to manage NULL field right ?* yes, obviously – Liam May 29 '20 at 13:45
  • Try using `codeAlgo?.IsBlank()` or (from guessing what IsBlank does) replace `codeAlgo.IsBlank() ? "" : codeAlgo;` with `codeAlgo ?? "";` – DaggeJ May 29 '20 at 13:46
  • Looking at your code I can see multiple places where this could be coming from, the following could be null and trigger this exception, `ListeDesGroupesNotationsItemVM`, `resultat.CodeAlgo`, `resultat.CodeSegment` or`worksheet.Cells[ligne, ...]` – Liam May 29 '20 at 13:49
  • I tried to comment some part and it works fine all the time except both lines `worksheet.Cells` for `resultat.codeAlgo` and `resultat.codeSegment` – Essex May 29 '20 at 13:51
  • DevBooster is a group framework. No one will know what it is here. Right now, what you are showing here is only C# using the Tools.Excel Microsoft lib. My guess is that @DaggeJ is right and you should remove `IsBlank()` function first. Then you should check `resultat`, `resultat.CodeAlgo` and `resultat.CodeSegment` which will throw if null. Some way to do it : `string codeAlgo = resultat?.CodeAlgo?.ToString();` string codeSegment = resultat?.CodeSegment?.ToString(); And same for every row. – Kirjava Oct 23 '20 at 16:31

0 Answers0