473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Range("I" + CStr(Row)).Value.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.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.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.InitialDirectory = "C:\"
o.FilterIndex = 2
o.RestoreDirectory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.DialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObject("Excel.Application"),
Microsoft.Office.Interop.Excel.Application)
xlApp.Visible = False
xlApp.Workbooks.Open(o.FileName)
xlWB = xlApp.ActiveWorkbook
xlSheet = CType(xlWB.Sheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

Dim strfilename As String = "logicaldevice.xml"

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

If strName IsNot Nothing Then
arlsName.Add(strName)
arlsAddress.Add(strAddress)
arlsType.Add(strType)
arlsFile.Add(strFile)
arlsEnc.Add(strEnc)
arlsSubnet.Add(strSubnet)
arlsTag.Add(strTag)
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.DialogResult.Cancel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exit()
End If
Jul 14 '06 #1
2 2693
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.Range("I" + CStr(Row)).Value.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.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.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.InitialDirectory = "C:\"
o.FilterIndex = 2
o.RestoreDirectory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.DialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObject("Excel.Application"),
Microsoft.Office.Interop.Excel.Application)
xlApp.Visible = False
xlApp.Workbooks.Open(o.FileName)
xlWB = xlApp.ActiveWorkbook
xlSheet = CType(xlWB.Sheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

Dim strfilename As String = "logicaldevice.xml"

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

If strName IsNot Nothing Then
arlsName.Add(strName)
arlsAddress.Add(strAddress)
arlsType.Add(strType)
arlsFile.Add(strFile)
arlsEnc.Add(strEnc)
arlsSubnet.Add(strSubnet)
arlsTag.Add(strTag)
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.DialogResult.Cancel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exit()
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********@discussions.microsoft.comschreef in bericht
news:B2**********************************@microsof t.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.Range("I" + CStr(Row)).Value.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.Office.Interop.Excel.Application
Dim xlWB As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.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.InitialDirectory = "C:\"
o.FilterIndex = 2
o.RestoreDirectory = True
o.Filter = "All Files (*.xls)|*.xls"

If (o.ShowDialog = Windows.Forms.DialogResult.OK) Then
lblDisplay.Text = o.FileName
xlApp = CType(CreateObject("Excel.Application"),
Microsoft.Office.Interop.Excel.Application)
xlApp.Visible = False
xlApp.Workbooks.Open(o.FileName)
xlWB = xlApp.ActiveWorkbook
xlSheet = CType(xlWB.Sheets(2),
Microsoft.Office.Interop.Excel.Worksheet)

Dim strfilename As String = "logicaldevice.xml"

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

If strName IsNot Nothing Then
arlsName.Add(strName)
arlsAddress.Add(strAddress)
arlsType.Add(strType)
arlsFile.Add(strFile)
arlsEnc.Add(strEnc)
arlsSubnet.Add(strSubnet)
arlsTag.Add(strTag)
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.DialogResult.Cancel) Then
MsgBox("You did not select any xls file, the program will now
close")
Application.Exit()
End If

Jul 16 '06 #3

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

Similar topics

2
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...
15
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...
6
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...
3
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...
3
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...
7
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...
4
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...
0
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...
12
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. ...
2
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.