vb.net中的对象引用错误

时间:2015-12-15 20:17:31

标签: vb.net winforms visual-studio

这是Windows窗体应用程序的一部分,它提示用户“请输入销售订单”,然后打开CAD图纸。但是,我在“dsDoc = dsApp.GetActiveDocument()”收到错误。任何人都可以帮我解决这个问题吗?谢谢!

 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

1 个答案:

答案 0 :(得分:0)

问题在于:

textA
textB(5,1,3)
textC(3)
textD
.
.

在这种情况下,对象引用是变量dsApp = Nothing ' ... dsDoc = dsApp.GetActiveDocument() 。如果它表示未设置为对象的实例,则表示该变量为空(即dsApp)。显然,由于您明确将变量设置为Nothing,因此它始终为null,因此可以保证抛出Nothing

要解决此问题,您需要将NullReferenceException设置为指向相应的dsApp对象,而不是将其设置为DraftSight.Application