|
-
Feb 4th, 2006, 10:36 PM
#1
Thread Starter
New Member
Object Refrence not set to an instnace of an object?
Hello forum... its my first post here and Im hoping you can help me with a small problem Im trying to solve. Im very new to programming so please bear with me. Anyway, as you can see below, Im getting the error 'Object Refrence not set to an instance of an object? Please let me know if you need the remainder of the code.
Imports System.IO
Imports Excel
Imports System.Net.Mail
Module Module1
Private mo_Wsheet As New Worksheet
Sub Main()
.....
End Sub
Private Sub RunChart(ByVal pi_NewWSheet As Integer, ByVal ps_ChrtSizeX As Integer, ByVal ps_ChrtSizeY As Integer, ByVal ps_ChrtLocX As Integer, _
ByVal ps_ChrtLocY As Integer, ByVal StartRange As String, ByVal EndRange As String)
'Object Refrence not set to an instnace of an object?
Dim lo_charts As Excel.ChartObjects = DirectCast(mo_Wsheet.ChartObjects(), Excel.ChartObjects)
......
End Module
-
Feb 4th, 2006, 11:30 PM
#2
Re: Object Refrence not set to an instnace of an object?
usually these types of errors when you don't use the "New" keyword in order to declare a new instance of an object, like "Dim lo_charts As New Excel.ChartObjects......" however not certain if that is the case here...
-
Feb 4th, 2006, 11:46 PM
#3
Re: Object Refrence not set to an instnace of an object?
As always with NullReferenceExceptions, when the exception is thrown you press the break button and then you test every reference on that line until you find the one that's Nothing. Once you know which it is you work backwards to find the place you expected that object to be created. If it is mo_Wsheet in this case then you are creating the object when you declare it, so the only possibility there is that you must be assigning Nothing to the variable somewhere else.
-
Feb 5th, 2006, 12:16 AM
#4
Thread Starter
New Member
Re: Object Refrence not set to an instnace of an object?
I will give it a try, thank you for your help.
-
Feb 5th, 2006, 12:29 AM
#5
Thread Starter
New Member
Re: Object Refrence not set to an instnace of an object?
-
Feb 5th, 2006, 12:31 AM
#6
Thread Starter
New Member
Re: Object Refrence not set to an instnace of an object?
wow... you were right, I had mo_Wsheet set to Nothing in another function. Amazing how you got that from what I gave you... Thank you!
-
Feb 5th, 2006, 12:48 AM
#7
Re: Object Refrence not set to an instnace of an object?
 Originally Posted by Ryedunn
wow... you were right, I had mo_Wsheet set to Nothing in another function. Amazing how you got that from what I gave you... Thank you!
Just to give you an idea of how you need to attack these issues, taking a look at the line that threw the exception:
VB Code:
Dim lo_charts As Excel.ChartObjects = DirectCast(mo_Wsheet.ChartObjects(), Excel.ChartObjects)
the only real possibility there is that mo_Wsheet is Nothing. It's the only reference you are trying to use to acces a member so it is the only place the exception could be thrown. You also posted this line:
VB Code:
Private mo_Wsheet As New Worksheet
that shows that you did assign an object to that variable when you created it. The logical conclusion is that somewhere between those two points you must have explicitly assigned Nothing to that variable, or else assigned some other variable to it that was itself Nothing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|