Ho
In oComany it shows null value. It gives error :Object Reference not set to an instance of the Object.
When i write code of PendingInvoice in Constructor it works fine
public partial class Form1 : Form
{
public static SAPbouiCOM.Application oApplication = null;
public static SAPbobsCOM.Company oCompany = null;
public static SAPbouiCOM.Form oForm = null;
public Form1()
{
//InitializeComponent();
SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();
oCompany.Server = "[email protected]:30015";
oCompany.DbServerType = BoDataServerTypes.dst_HANADB;
oCompany.CompanyDB = "TestE";
oCompany.UserName = "manager";
oCompany.Password = "123";
oCompany.language = BoSuppLangs.ln_English;
oCompany.UseTrusted = true;
int connectionResult = oCompany.Connect();
if (connectionResult != 0)
{
throw new Exception("Error connecting to SAP Business One: " + oCompany.GetLastErrorDescription());
}
PendingInvoice();
}
private void PendingInvoice()
{
string _StrSQL = "SELECT 'Invoice',T0.\"TransId\", T0.\"DocNum\", T0.\"DocDate\", T0.\"BPLName\", T0.\"CardCode\" " +
", T0.\"CardName\", T0.\"DocTotal\", T4.\"U_UTL_IRN\" " +
"FROM \"Test\".OINV T0 " +
"INNER JOIN CRD1 T3 ON T0.\"CardCode\" = T3.\"CardCode\" " +
"INNER JOIN INV12 T5 ON T5.\"DocEntry\" = T0.\"DocEntry\" " +
"LEFT JOIN \"Test\".\"@AUTL_MDEXTH\" T4 ON T0.\"DocEntry\" = T4.\"U_UTL_BaseEntry\" " +
"where T0.\"CardCode\" = T3.\"CardCode\" and T0.\"PayToCode\" = T3.\"Address\" and T3.\"AdresType\" = 'B' and T3.\"GSTRegnNo\" <> '' " +
"and T0.\"DocEntry\" NOT IN(Select A4.\"U_UTL_BaseEntry\" from \"test\".\"@AUTL_MDEXTH\" A4 where A4.\"U_UTL_IST\" <> 'F' ) " +
"and T5.\"LocGSTN\" <> T5.\"BpGSTN\" and T0.\"BPLId\" = '28'" + "";
SAPbobsCOM.Recordset oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
oRecordSet.DoQuery(_StrSQL);
}
}
Thanks
Prasad RaveendranPosted Mar 16, 2025, 5:49 AM
Why can't you try this refactored code post your comments
Eliana BlakePosted Mar 15, 2025, 7:49 AM
It seems like the issue with `oCompany` being null and throwing an "Object Reference not set to an instance of the Object" error arises from the redeclaration of `oCompany` within the constructor of your `Form1` class.
In your code snippet, you have `SAPbobsCOM.Company oCompany = new SAPbobsCOM.Company();` within the constructor. This creates a new local variable `oCompany` within the constructor scope, which is distinct from the static class variable `oCompany` declared at the class level. As a result, the static `oCompany` remains null, leading to the error when you try to access it in the `PendingInvoice` method.
To resolve this issue, you should remove the type declaration `SAPbobsCOM.Company` when initializing `oCompany` within the constructor. Simply use `oCompany = new SAPbobsCOM.Company();` to initialize the static variable instead of redeclaring it. This way, you will be modifying the static `oCompany` variable, ensuring it is properly initialized and accessible throughout your class.
By making this adjustment, you should no longer encounter the null reference error, and your `PendingInvoice` method should be able to utilize the initialized `oCompany` instance successfully.
If you need further clarification or assistance, feel free to ask!