3therk1ll
3therk1ll

Reputation: 2421

TypeInitializationException with EPPlus script

I am trying to run a script to read values from an Excel sheet using EPPlus and load them into a list of tuples. However when I run the script I am getting two errors, the first is:

An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll

I have seen in other posts that I need to check for the inner exception, however none is offered by Visual Studio 15. This is all the exception details that are available.

System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'CGCompare2.Program' threw an exception.

Then when I close the VS15 exception window I get a pop up dialogue:

Cannot access a disposed object.
Object name: 'HwndSourceAdapter'

I am unsure what the issue is, if this is caused by my code or not. Any help, much appreciated.

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;

namespace CGComparer
{
    class Program
    {
        private static List<Tuple<string, string>> _listTop;
        private static List<Tuple<string, string>> _listGNED;
        private static Base _baseCell;
        private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
        private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "Compare GNED and TOP V1.0.xlsx");

        static void Main(string[] args)
        {
            _baseCell = new Base(1, 2);
            _listTop = ColumnsToList(_baseCell.Column(),_baseCell.Row());

            _baseCell = new Base(3, 2);
            _listGNED = ColumnsToList(_baseCell.Column(),_baseCell.Row());
        }

        public static List<Tuple<string, string>> ColumnsToList(int column, int row)
        {
            var list = new List<Tuple<string, string>>();
            var ws = _package.Workbook.Worksheets[1];
            var ListIsValid = true;
            do
            {
                var userEmail = (string)ws.Cells[column, row].Value;
                var customerGroup = (string)ws.Cells[column + 1, row].Value;

                if (!string.IsNullOrEmpty(userEmail))
                {
                    list.Add(new Tuple<string, string>(userEmail, customerGroup));
                    row = row++;
                }
                else
                {
                    ListIsValid = false;
                }

            } while (ListIsValid);

            return list;
        }
    }
}

Base.cs

namespace CGComparer
{
    public class Base
    {
        private static int _column;
        private static int _row;

        public Base(int column, int row)
        {
            _column = column;
            _row = row;
        }

        public int Column()
        {
            return _column;
        }

        public int Row()
        {
            return _row;
        }
    }
}

Upvotes: 0

Views: 502

Answers (1)

3therk1ll
3therk1ll

Reputation: 2421

So, turns out the issue was staring me in the face, I went through below steps provided by, Hans Passant in the issue comments:

"The debugger in VS2015 is a crappy bag 'o bugs, it won't let you look at the InnerException. Use Tools > Options > Debugging > General > tick "Use Managed Compatibility Mode" and now you can see it. Careful with those statics, their initializer can byte you in the rear end badly."

It was a null reference exception caused by me declaring the excel file with a path argument that had yet to be declared itself.

private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile));
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "Compare GNED and TOP V1.0.xlsx");

Upvotes: 1

Related Questions