473,883 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object reference not set to an instance of an object error

i got this program that will fetch the data in the excel spreadsheet, it was
working before then i make some adjustment and it now give me an error of
"Object reference not set to an instance of an object error" i can't seem to
see what's wrong with my code so i'm hoping some fresh eyes can spot what's
wrong or what i'm missing. the following is the code, this line strFile =
CStr(xlSheet.Ra nge("I" + CStr(Row)).Valu e.ToString + "\" + strfilename) gave
me the object reference not set error. it suppose to fetch the data from
spreadsheet and tell it what directory to copy the file from the I column of
the spreadsheet contain the directory on a server.

any help is much appericated

thank

Dim o As OpenFileDialog
Dim xlApp As Microsoft.Offic e.Interop.Excel .Application
Dim xlWB As Microsoft.Offic e.Interop.Excel .Workbook
Dim xlSheet As Microsoft.Offic e.Interop.Excel .Worksheet
Dim strName As String
Dim strAddress As String
Dim strType As String
Dim strFile As String
Dim strEnc As String
Dim strSubnet As String
Dim strTag As String
Dim strEventP As String
Dim strEventYN As String
Dim Row As Integer = 2

arlsName = New ArrayList
arlsAddress = New ArrayList
arlsType = New ArrayList
arlsFile = New ArrayList
arlsEnc = New ArrayList
arlsSubnet = New ArrayList
arlsTag = New ArrayList
arlsEventP = New ArrayList
arlsEventYN = New ArrayList

o = New OpenFileDialog
o.Title = "Open Excel spreadsheet"
o.InitialDirect ory = "C:\"
o.FilterIndex = 2
o.RestoreDirect ory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.D ialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObj ect("Excel.Appl ication"),
Microsoft.Offic e.Interop.Excel .Application)
xlApp.Visible = False
xlApp.Workbooks .Open(o.FileNam e)
xlWB = xlApp.ActiveWor kbook
xlSheet = CType(xlWB.Shee ts(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

Dim strfilename As String = "logicaldevice. xml"

Try
Do
strName = CStr(xlSheet.Ra nge("A" + CStr(Row)).Valu e)
strAddress = CStr(xlSheet.Ra nge("C" + CStr(Row)).Valu e)
strType = CStr(xlSheet.Ra nge("F" + CStr(Row)).Valu e)
strFile = CStr(xlSheet.Ra nge("I" +
CStr(Row)).Valu e.ToString + "\" + strfilename)
strEnc = CStr(xlSheet.Ra nge("E" + CStr(Row)).Valu e)
strSubnet = CStr(xlSheet.Ra nge("D" + CStr(Row)).Valu e)
strTag = CStr(xlSheet.Ra nge("H" + CStr(Row)).Valu e)
strEventP = CStr(xlSheet.Ra nge("L" + CStr(Row)).Valu e)
strEventYN = CStr(xlSheet.Ra nge("K" + CStr(Row)).Valu e)

If strName IsNot Nothing Then
arlsName.Add(st rName)
arlsAddress.Add (strAddress)
arlsType.Add(st rType)
arlsFile.Add(st rFile)
arlsEnc.Add(str Enc)
arlsSubnet.Add( strSubnet)
arlsTag.Add(str Tag)
arlsEventP.Add( strEventP)
arlsEventYN.Add (strEventYN)
End If
Row += 1
Loop Until strName = ""

Catch ex As Exception
MessageBox.Show (ex.ToString)
End Try

xlWB.Close()
xlApp.Quit()

ElseIf (o.ShowDialog = Windows.Forms.D ialogResult.Can cel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exi t()
End If
Jul 14 '06 #1
2 2712
Looks to me like you need to remove the .ToString from the offending line.
Look at the line just above it, which apparently works and which is a string
to which you can append "\" & strfilename.
--
Terry
"dotnetnoob " wrote:
i got this program that will fetch the data in the excel spreadsheet, it was
working before then i make some adjustment and it now give me an error of
"Object reference not set to an instance of an object error" i can't seem to
see what's wrong with my code so i'm hoping some fresh eyes can spot what's
wrong or what i'm missing. the following is the code, this line strFile =
CStr(xlSheet.Ra nge("I" + CStr(Row)).Valu e.ToString + "\" + strfilename) gave
me the object reference not set error. it suppose to fetch the data from
spreadsheet and tell it what directory to copy the file from the I column of
the spreadsheet contain the directory on a server.

any help is much appericated

thank

Dim o As OpenFileDialog
Dim xlApp As Microsoft.Offic e.Interop.Excel .Application
Dim xlWB As Microsoft.Offic e.Interop.Excel .Workbook
Dim xlSheet As Microsoft.Offic e.Interop.Excel .Worksheet
Dim strName As String
Dim strAddress As String
Dim strType As String
Dim strFile As String
Dim strEnc As String
Dim strSubnet As String
Dim strTag As String
Dim strEventP As String
Dim strEventYN As String
Dim Row As Integer = 2

arlsName = New ArrayList
arlsAddress = New ArrayList
arlsType = New ArrayList
arlsFile = New ArrayList
arlsEnc = New ArrayList
arlsSubnet = New ArrayList
arlsTag = New ArrayList
arlsEventP = New ArrayList
arlsEventYN = New ArrayList

o = New OpenFileDialog
o.Title = "Open Excel spreadsheet"
o.InitialDirect ory = "C:\"
o.FilterIndex = 2
o.RestoreDirect ory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.D ialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObj ect("Excel.Appl ication"),
Microsoft.Offic e.Interop.Excel .Application)
xlApp.Visible = False
xlApp.Workbooks .Open(o.FileNam e)
xlWB = xlApp.ActiveWor kbook
xlSheet = CType(xlWB.Shee ts(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

Dim strfilename As String = "logicaldevice. xml"

Try
Do
strName = CStr(xlSheet.Ra nge("A" + CStr(Row)).Valu e)
strAddress = CStr(xlSheet.Ra nge("C" + CStr(Row)).Valu e)
strType = CStr(xlSheet.Ra nge("F" + CStr(Row)).Valu e)
strFile = CStr(xlSheet.Ra nge("I" +
CStr(Row)).Valu e.ToString + "\" + strfilename)
strEnc = CStr(xlSheet.Ra nge("E" + CStr(Row)).Valu e)
strSubnet = CStr(xlSheet.Ra nge("D" + CStr(Row)).Valu e)
strTag = CStr(xlSheet.Ra nge("H" + CStr(Row)).Valu e)
strEventP = CStr(xlSheet.Ra nge("L" + CStr(Row)).Valu e)
strEventYN = CStr(xlSheet.Ra nge("K" + CStr(Row)).Valu e)

If strName IsNot Nothing Then
arlsName.Add(st rName)
arlsAddress.Add (strAddress)
arlsType.Add(st rType)
arlsFile.Add(st rFile)
arlsEnc.Add(str Enc)
arlsSubnet.Add( strSubnet)
arlsTag.Add(str Tag)
arlsEventP.Add( strEventP)
arlsEventYN.Add (strEventYN)
End If
Row += 1
Loop Until strName = ""

Catch ex As Exception
MessageBox.Show (ex.ToString)
End Try

xlWB.Close()
xlApp.Quit()

ElseIf (o.ShowDialog = Windows.Forms.D ialogResult.Can cel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exi t()
End If
Jul 14 '06 #2
DotNetNoob,

Set in top of your program
Option Strict On,

than it will probably give it you at design time.

I see this because in VBNet is this not done.

"A" + whatever

The concatination character is

"A" & whatever

This + can give errors with option strict of.

Cor

"dotnetnoob " <do********@dis cussions.micros oft.comschreef in bericht
news:B2******** *************** ***********@mic rosoft.com...
>i got this program that will fetch the data in the excel spreadsheet, it
was
working before then i make some adjustment and it now give me an error of
"Object reference not set to an instance of an object error" i can't seem
to
see what's wrong with my code so i'm hoping some fresh eyes can spot
what's
wrong or what i'm missing. the following is the code, this line strFile
=
CStr(xlSheet.Ra nge("I" + CStr(Row)).Valu e.ToString + "\" + strfilename)
gave
me the object reference not set error. it suppose to fetch the data from
spreadsheet and tell it what directory to copy the file from the I column
of
the spreadsheet contain the directory on a server.

any help is much appericated

thank

Dim o As OpenFileDialog
Dim xlApp As Microsoft.Offic e.Interop.Excel .Application
Dim xlWB As Microsoft.Offic e.Interop.Excel .Workbook
Dim xlSheet As Microsoft.Offic e.Interop.Excel .Worksheet
Dim strName As String
Dim strAddress As String
Dim strType As String
Dim strFile As String
Dim strEnc As String
Dim strSubnet As String
Dim strTag As String
Dim strEventP As String
Dim strEventYN As String
Dim Row As Integer = 2

arlsName = New ArrayList
arlsAddress = New ArrayList
arlsType = New ArrayList
arlsFile = New ArrayList
arlsEnc = New ArrayList
arlsSubnet = New ArrayList
arlsTag = New ArrayList
arlsEventP = New ArrayList
arlsEventYN = New ArrayList

o = New OpenFileDialog
o.Title = "Open Excel spreadsheet"
o.InitialDirect ory = "C:\"
o.FilterIndex = 2
o.RestoreDirect ory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.D ialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObj ect("Excel.Appl ication"),
Microsoft.Offic e.Interop.Excel .Application)
xlApp.Visible = False
xlApp.Workbooks .Open(o.FileNam e)
xlWB = xlApp.ActiveWor kbook
xlSheet = CType(xlWB.Shee ts(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

Dim strfilename As String = "logicaldevice. xml"

Try
Do
strName = CStr(xlSheet.Ra nge("A" + CStr(Row)).Valu e)
strAddress = CStr(xlSheet.Ra nge("C" + CStr(Row)).Valu e)
strType = CStr(xlSheet.Ra nge("F" + CStr(Row)).Valu e)
strFile = CStr(xlSheet.Ra nge("I" +
CStr(Row)).Valu e.ToString + "\" + strfilename)
strEnc = CStr(xlSheet.Ra nge("E" + CStr(Row)).Valu e)
strSubnet = CStr(xlSheet.Ra nge("D" + CStr(Row)).Valu e)
strTag = CStr(xlSheet.Ra nge("H" + CStr(Row)).Valu e)
strEventP = CStr(xlSheet.Ra nge("L" + CStr(Row)).Valu e)
strEventYN = CStr(xlSheet.Ra nge("K" + CStr(Row)).Valu e)

If strName IsNot Nothing Then
arlsName.Add(st rName)
arlsAddress.Add (strAddress)
arlsType.Add(st rType)
arlsFile.Add(st rFile)
arlsEnc.Add(str Enc)
arlsSubnet.Add( strSubnet)
arlsTag.Add(str Tag)
arlsEventP.Add( strEventP)
arlsEventYN.Add (strEventYN)
End If
Row += 1
Loop Until strName = ""

Catch ex As Exception
MessageBox.Show (ex.ToString)
End Try

xlWB.Close()
xlApp.Quit()

ElseIf (o.ShowDialog = Windows.Forms.D ialogResult.Can cel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exi t()
End If

Jul 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10551
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server Error in '/Cr_Dataset' Application. ----------------------------------------------------------- ---------------------
15
6756
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
6
6133
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page then tried to access 2nd, or 3rd, etc page. The problem is it happens sometimes - sometimes when they clicked refresh button, then everything is ok. Now they told me it happens more frequently. but I have tried by myself many times and never got...
3
455
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but why shouldn't it happen in the localhost? Here's what the the whole error msg looks like:
3
4239
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
7
1576
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As Integer = 0 To LinkResults.Length - 1 With MyCurrentClass.SqlCmd_Insert_LinkVB .CommandType = System.Data.CommandType.StoredProcedure
4
1890
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in browsers) ? What about the new syntax of creating a object using { 'propName1':'propValue1', 'propName2':'propValue2', 'propName3':{ /* another object */ } } - from what version of JScript/JavaScript it was supported (from what browsers versions) ?...
0
1880
by: cnSoftware | last post by:
I come across this problem during my recent project and even now I am still be puzzled by it. So I paste the procedure here. I wish you can figure out where I am wrong or missing. The sample application is developed with VS 2005 Beta 2. 1. Create a base class named ¡°BaseEntity¡±
12
5564
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
2
2664
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
9943
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10750
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.