VB.NET 2005 object refernce?

I keep this error: object reference not set to an instance of an object

List exists and everything is in it. The object here is to load the column heading from list onto the excel spread sheet. Using the offset method from the range class,which is part of the sheet class

Public Sub populateColumnHeadings(ByVal namedSheet As String, ByVal ojbBook As Object)

Dim i As Integer = 1
Dim c As Integer = 0
Dim r As Integer = 0

Try

Dim xls As Excel.Worksheet = CType(objBook.Sheets(namedSheet), Excel.Worksheet)

Dim rng As Excel.Range = CType(xls.Cells(1, 1), Excel.Range) 'range starts a "A1"

rng = CType(xls.Cells(1, 1), Excel.Range)


With xls
For Each columnName1 As String In headingList
rng.Offset(r, c).Value = columnName1
c += 1

Next

i += 1
c = 0

End With

Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub

Any suggestions?

Thank you
[1219 byte] By [Alisa] at [2007-11-19 18:05:25]
# 1 Re: VB.NET 2005 object refernce?
You should let us know which line the exception was thrown on. It may be obvious or it may not, but either way it's not much trouble for you to point it out. Also, you can work out exactly which reference is Nothing, not just which line it occurs on. Set a breakpoint at the top of the code. Step through it line by line using F10. When you get to the line that throws the exception test every reference on it using the Autos, Locals and Watch windows. Once you know which reference is Nothing then you can work backwards to the point you think that object should have been created or assigned. If you still can't work it out then you post the question. Also, could you use [code] tags when posting code? It makes it so much easier to read. Try editing your original post, select the code and press the (#) button. You'll have to reapply the indenting yourself now, but if you do it from the start your code will maintain indenting and use a fixed-width font.
jmcilhinney at 2007-11-10 3:14:54 >
# 2 Re: VB.NET 2005 object refernce?
i think have not declared new instances of Excel.Worksheet and Excel.Range
aniskhan at 2007-11-10 3:15:56 >
# 3 Re: VB.NET 2005 object refernce?
Well usually these types of errors occur when you don't specify the "New" keyword in the constructor of the line that the error was on... like...

'did this
Dim MyTextBox as TextBox

'instead of doing it this way

Dim MytextBox as New TextBox

Of course, this is a general example...
gigemboy at 2007-11-10 3:16:54 >
# 4 Re: VB.NET 2005 object refernce?
if the error is pointed in the decleration of the sub...

Public Sub populateColumnHeadings(ByVal namedSheet As String, ByVal ojbBook As Object)

then i think that the "objBook" has not been instantiated using New key word.....try sending an instantiated object in the call

or tyy the following

Dim xls As New Excel.Worksheet = CType(objBook.Sheets(namedSheet), Excel.Worksheet)

Dim rng As New Excel.Range = CType(xls.Cells(1, 1), Excel.Range) 'range starts a "A1"
AamirHabib at 2007-11-10 3:17:52 >