0

I'd like to use a constructor to establish objects for my class. The problem is that the constructor executes properly (stepped over each assignment within the constructor in the Visual Studio debugger), however after the constructor finishes and the object is established, I can't use any of my class's methods to access the data members.

There seems to be a disconnect between the data members listed above the constructor and the data members assigned inside the constructor.

The error that posts is: "NullReferenceException Unhandled - Object reference not set to an instance of an object."

...
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;

namespace ExcelManip
{
    class ExcelInterop
    {
        //MEMBERS
        private Application _excelApp;// = new Application();
        private Workbooks books;
        private Workbook workBook;

        //CONSTRUCTOR
        public ExcelInterop(string thisFileName)
        {
            Application _excelApp = new Application();
            Workbooks books = _excelApp.Workbooks;
            Workbook workBook  = books.Open(thisFileName,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing);
        }
John Saunders
  • 157,405
  • 24
  • 229
  • 388
sm1
  • 13
  • 1
  • 5
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 03 '13 at 22:48
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 03 '13 at 22:53
  • Appreciate the edit John and the hint, the trouble I was having is that I thought I was initializing the variables through the constructor, but I was really recreating the variables... thanks again! – sm1 Jun 03 '13 at 22:58

1 Answers1

0

You are initiating new variables in your constructor's scope, not your new object instance's scope. Change to this:

        public ExcelInterop(string thisFileName)
        {
            _excelApp = new Application();
            books = _excelApp.Workbooks;
            workBook  = books.Open(thisFileName,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing);
        }
Alex Polkhovsky
  • 3,208
  • 5
  • 26
  • 36
  • Looks right to me :) , so I was basically creating two separate sets of the same members, one set only available to the constructor scope and one to the class scope? Trying it out now, thanks! – sm1 Jun 03 '13 at 22:49