-3

This is part of a windows form application which prompts a user to "Please enter a sales order" and then will open up a CAD drawing. However, I'm getting an error at "dsDoc = dsApp.GetActiveDocument()". Can anyone help me out with this one? Thank you!

 Private Sub startButton_Click(sender As Object, e As EventArgs) Handles startButton.Click
    Dim salesOrder As Integer
    If Int32.TryParse(txtboxSalesOrder.Text, salesOrder) Then
        If salesOrder > 99999 AndAlso salesOrder <= 999999 Then
            Using process As Process = New Process
                Dim ProcessProperties As New ProcessStartInfo
                ProcessProperties.FileName = "C:\Program Files\Dassault Systemes\DraftSight\bin\DraftSight.exe"
                ProcessProperties.Arguments = ("C:\Users\MyUserName\Desktop\JE5022AA.dwg")
                ProcessProperties.WindowStyle = ProcessWindowStyle.Hidden
                process.Start(ProcessProperties)
            End Using
            Dim dsApp As DraftSight.Application
            Dim application As Object
            dsApp = Nothing
            application = Nothing

            Dim dsDoc As DraftSight.Document
            dsDoc = dsApp.GetActiveDocument()
           'An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe
           'Additional information: Object reference not set to an instance of an object.

            If dsDoc Is Nothing Then
                Return
            End If
Bcon615
  • 5
  • 4
  • 1
    well, just a few lines before you have `dsApp = Nothing` and nothing in the next 3-4 lines initializes it, so `dsApp` is till Nothing/null when you try to `GetActiveDocument()` from it – Ňɏssa Pøngjǣrdenlarp Dec 15 '15 at 20:19

1 Answers1

0

Here's the problem:

dsApp = Nothing
' ...
dsDoc = dsApp.GetActiveDocument()

In this case, the Object reference is the variable dsApp. When it says that it's not set to an instance of an object, that means that the variable is null (i.e. Nothing). Obviously, since you are explicitly setting the variable to Nothing, it will always be null, so it is guaranteed to throw a NullReferenceException.

To fix it, you need to set the dsApp to point to the appropriate DraftSight.Application object rather than setting it to Nothing.

Steven Doggart
  • 41,612
  • 8
  • 64
  • 99
  • That was automatically generated from recording a macro earlier on. Do you have any suggestions as to what "dsApp" should be set equal to? Thanks! – Bcon615 Dec 15 '15 at 20:21
  • Since I'm not sure what `DraftSight.Application` is, I couldn't say. I assume that's a type imported from some third-party library. It probably assumes that you will already have a reference to the current `DraftSight.Application` object somewhere and you just need to set `dsApp` to point to that object. But since I've never used or heard of the library, that's just a guess. – Steven Doggart Dec 15 '15 at 20:24
  • you could try changing the line earlier to `Dim dsApp As New DraftSight.Application` (and remove the one setting it to Nothing) but, yea we have no idea what you are doing or working with – Ňɏssa Pøngjǣrdenlarp Dec 15 '15 at 20:26
  • You could consult the [DraftSight SDK Documentation](http://www.3ds.com/support/documentation/developers-guides/). I can't read their documentation for you because it requires a valid client login. – Steven Doggart Dec 15 '15 at 20:29
  • @Plutonix For the pedantic at heart: [Yea](http://public.wsu.edu/~brians/errors/yea.html) – Steven Doggart Dec 15 '15 at 20:32
  • Or, you could close this daily dupe. – Bjørn-Roger Kringsjå Dec 15 '15 at 20:35
  • @Bjørn-RogerKringsjå I was the first to cast my vote. What are you waiting for? – Steven Doggart Dec 15 '15 at 20:36
  • 1
    @Bjørn-RogerKringsjå Ah, there it is :) Good call. Wish I had thought to use the Dupe-Hammer with that canonical answer... Oh well. – Steven Doggart Dec 15 '15 at 20:39
  • @Plutonix Setting it to 'Dim dsApp As New DraftSight.Application' worked perfectly, thank you. – Bcon615 Dec 15 '15 at 20:40