cancel
Showing results for 
Search instead for 
Did you mean: 

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

tichaona_gaza
Participant
0 Kudos

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

Accepted Solutions (0)

Answers (1)

Answers (1)

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

tichaona_gaza
Participant
0 Kudos

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

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

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

Kind regards,

ANKIT CHAUHAN

SAP SME Support

tichaona_gaza
Participant
0 Kudos

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