cancel
Showing results for 
Search instead for 
Did you mean: 

Error calling RFC - Object reference not set to an instance of an object

Former Member
0 Kudos

Hello,

I've written a .NET program that uses the SAP .NET Connector version 1. I'm using VB.NET and proxy generated in C++ to successfully log on to SAP. SAPComCode1 is the proxy.

The error is thrown when I attempt to call an RFC. The RFC is one that I wrote to create PM orders. The error references the following line in the proxy: result = this.SAPInvoke("Z_Create_Pm_Order", new object[] (Gstrp,Iilart,Iwerk,Ktext,Pm_Aufart,Vaplz);

My vb code is as follows:

Dim objSAPPMOrderCreate As New SAPComCode1.SAPComCode1

' sent to SAP

Dim Gstrp As String

Dim Iilart As String

Dim Iwerk As String

Dim Ktext As String

Dim Pm_Aufart As String

Dim Vaplz As String

' output from SAP

Dim ErrMsg As String

Dim Order As String

' set variables based on user input

Gstrp = CStr(dtpStartDate.Text)

Iilart = Microsoft.VisualBasic.Left(cboActivityType.Text, 3)

Iwerk = Microsoft.VisualBasic.Left(cboPlant.Text, 4)

Ktext = Microsoft.VisualBasic.Left(txtShortText.Text, 40)

Pm_Aufart = Microsoft.VisualBasic.Left(cboOrderType.Text, 4)

Vaplz = cboWorkCenter.Text

' call rfc via proxy

objSAPPMOrderCreate.Z_Create_Pm_Order(Gstrp, Iilart, Iwerk, Ktext, Pm_Aufart, Vaplz, ErrMsg, Order)

Probably fairly obviously, I'm an Abapper new to VB & .NET. I did review the samples installed with the .NET Connector. Using a tutorial by Thomas Schuessler, I also successfully called Bapi_Companycode_Getlist in C.

Thanks so much in advance for your help!

Laura Andrews

Accepted Solutions (0)

Answers (1)

Answers (1)

reiner_hille-doering
Active Contributor
0 Kudos

It seems that you neither specify a connection nor a connection string to your proxy "objSAPPMOrderCreate". So the proxy doesn't know where to connect to.

Tip: Use SAP .NET Connector 2.0 and try one of the samples in the documentation. The newer version has better VB support and allows you to configure the proxy and the destination it should use in the designer.

Former Member
0 Kudos

Thank you for the reply. I should have mentioned that the connection and connection string are established in with a different form and in a different subroutine. The connection is successfully established by this subroutine. Using tranaction code sm04 I can see that the rfc logon is successful. Are you saying that it is necessary to both make the connection and call the rfc in the same subroutine? I had separated them into a logon form and a data entry form. Below are the commands used to establish the connection:

' Change cursor to hourglass

Cursor.Current = Cursors.WaitCursor

' Initialize message

lblLogonMessage.Text = " "

' Create an instance of an automation server

Dim objSAPConnect As New SAPComCode1.SAPComCode1

' Set logon variables

Destination1.Client = cboClient.Text

Destination1.Username = txtUser.Text

Destination1.Password = txtPassword.Text

Destination1.Language = "EN"

Try

' Logon to SAP

objSAPConnect.Connection() = New SAP.Connector.SAPConnection(Destination1)

objSAPConnect.Connection.Open()

Catch ex As Exception

' Check for successful connection

lblLogonMessage.Text = "Error in logging on SAP: " & ex.Message

Finally

If lblLogonMessage.Text = " " Then

lblLogonMessage.Text = "Successfully logged on SAP (for running remote function calls)"

' Display PM Order Create form

Dim frmSAPPMOrder As New fclsSAPPMOrderCreate

frmSAPPMOrder.Show()

End If

' Change cursor to normal

Cursor.Current = Cursors.Default

End Try

I spent quite a lot of time trying to get the installation of 2.0 to work, including deleting the file you recommended to be deleted. This caused some other problems so that the operating system on my computer needed to be reinstalled. I appreciate your help.

Sincerely,

Laura Andrews

reiner_hille-doering
Active Contributor
0 Kudos

You can do the connect in a different function, but this line (in your first post) create a fresh new proxy without a set connection:

Dim objSAPPMOrderCreate As New SAPComCode1.SAPComCode1

Note that the connection is a member a the proxy instance, so after this line, "objSAPPMOrderCreate" doesn't have a connection set, regardless of what you did before with a different proxy instance.

I would recommend that you keep either the proxy or the connection as a member variable of your object. This is btw. exactly what happens if you drag a proxy icon from the toolbox.

Former Member
0 Kudos

That fixed it. Thanks so much for your patience and help!