Public Class ScoutFormMain Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range 'THIS CAUSES THE ERRORS 'define the filename of the Excel sheet, as defined by the user. Dim filename As String = txtFile.Text Dim filepath As String = "C:\Users\%username%\Desktop\" & txtFile.Text & ".xls" ...........
As you can see, I have the Excel stuff and the two variables as global variables, whereas the rest of the Excel-related code is under the "Private Sub btnEnterData" procedure that I have later on in the code. When i run the program from the IDE, it gives me this error:
System.InvalidOperationException was unhandled Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object. Source=ScoutForm StackTrace: at ScoutForm.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at ScoutForm.My.MyProject.MyForms.get_ScoutFormMain() at ScoutForm.My.MyApplication.OnCreateMainForm() in C:\Users\Robert\Documents\My Dropbox\ScoutForm\ScoutForm\My Project\Application.Designer.vb:line 35 at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String commandLine) at ScoutForm.My.MyApplication.Main(String Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.NullReferenceException Message=Object reference not set to an instance of an object. Source=ScoutForm StackTrace: at ScoutForm.ScoutFormMain..ctor() in C:\Users\Robert\Documents\My Dropbox\ScoutForm\ScoutForm\ScoutFormMain.vb:line 72 InnerException:
I've narrowed the problem to the "filename" and "filepath" variables. I've Googled around for answers, but the articles that I found about the error above are too vague to help me. Does anyone know why VB.NET is giving me this error? Also, how can I reuse the same Excel sheet for the entire duration that my VB.NET application is running? As in, I want to be able to add ~50 or so teams to the same Excel document (for example, all of it going into "MyRegional.xls").
Sorry if this is an obvious problem to solve or anything; I'm fairly new to programming in general, so I'm clueless as to what's going on with this. Up until this point, my program runs fine and when I comment out "Dim filename..." and "Dim filepath...", the program will run normally. If it helps, I'm running VB.NET Express 2010 in Windows 7 Home. Thank you SO much in advance =)