Skip to Content
0

"object reference not set to an instance of an object" error when tryin to post JE

May 11, 2017 at 07:56 AM

118

avatar image

Hi guys,

I keep getting this error "object reference not set to an instance of an object" when i run my code below. Can anyone help me out.

Public Class Form1
Dim oCompany As SAPbobsCOM.Company
Dim oJournal As SAPbobsCOM.JournalEntries
Dim oOrderDoc As SAPbobsCOM.Documents
Dim oBP As SAPbobsCOM.BusinessPartners
Dim OrdCodeString As String
Dim InvCodeString As String
Dim lRetCode, lErrCode As Long
Dim sErrMsg As String

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try

oCompany = New SAPbobsCOM.Company
oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2012
oCompany.Server = "DESKTOP-7T8DIME"
oCompany.CompanyDB = "DB_Woolly_Furniture"
oCompany.DbUserName = "sa"
oCompany.DbPassword = "Password123"
oCompany.UserName = "manager"
oCompany.Password = "manager"
If oCompany.Connect = 0 Then
MsgBox("Connected")
Else
MsgBox(oCompany.GetLastErrorDescription)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
oJournal = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalEntries)
oJournal.DueDate = Now
oJournal.TaxDate = Now
oJournal.Memo = "MY SDK Journal"

oJournal.Lines.AccountCode = "_SYS00000000018"
oJournal.Lines.Credit = 250
oJournal.Lines.LineMemo = "My line memo"
oJournal.Lines.Add()

oJournal.Lines.AccountCode = "_SYS00000000287"
oJournal.Lines.Debit = 250
oJournal.Lines.Add()


lRetCode = oJournal.Add()
If lRetCode <> 0 Then
oCompany.GetLastError(lErrCode, sErrMsg)
MsgBox("Error: " & lErrCode & ": " & sErrMsg)
Else
oCompany.GetNewObjectCode(OrdCodeString)
MsgBox("Added Journal no : " & OrdCodeString)
End If
Catch ex As Exception
MsgBox("Exception: " & ex.Message)
End Try
End Sub
End Class

Regards

Tichaona

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

ANKIT CHAUHAN
May 11, 2017 at 08:36 AM
0

Hi,

Can you please make sure that you are connected to the company successfully?

Also please note that each object has been instantiated correctly which you are refering in your code.

Otherwise, tell us the line on which you are getting the error.

Kind regards,

ANKIT CHAUHAN

SAP SME Support

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Its connecting just fine. Thats why am so totally confused.

0

Hi,

Which line in your project code is giving you the error?

Kind regards,

ANKIT CHAUHAN

SAP SME Support

0

Hi Ankit

A colleague of mine just noticed that i declared oCompany twice, globally and in the button. That's why it wasn't working for the other code below that. Thanks for your help.

Regards

Tichaona

0