Results 1 to 7 of 7

Thread: Object Refrence not set to an instnace of an object?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Question 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

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Re: Object Refrence not set to an instnace of an object?

    I will give it a try, thank you for your help.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Re: Object Refrence not set to an instnace of an object?

    seems like all of

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    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!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Object Refrence not set to an instnace of an object?

    Quote 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:
    1. 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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width