Solved

Null value Exception when Inserting to Cache

  • 23 June 2023
  • 4 replies
  • 127 views

Userlevel 3
Badge

Hi Team,

I’m getting Object reference error when inserting to Cache. Though I’m setting InvoiceCust value , exception is thrown stating InvoiceCust is null. Please see the code snippet below. Am doing anything wrong here?

 

 XRBContrDet newSchedule = (XRBContrDet)ContractDetails.Cache.CreateCopy((XRBContrDet)OldSchedule);
            newSchedule.ContractID = NewContract.ContractID;
            newSchedule.LineNbr = null;
            newSchedule.NoteID = null;
            newSchedule.CuryAmount = 0;
            newSchedule.CuryPrePayBal = 0;
            newSchedule.InvoiceCust = OldSchedule.InvoiceCust;         

            newSchedule = ContractDetails.Insert(newSchedule);

 

View

 [PXCopyPasteHiddenFields(new Type[] { typeof(XRBContrDet.manuallyBooked), typeof(XRBContrDet.aRRefNbr), typeof(XRBContrDet.sOOrdNbr) })]
        public PXSelectJoin<XRBContrDet,
            LeftJoin<ARInvoice, On<ARInvoice.refNbr, Equal<XRBContrDet.aRRefNbr>>>,
            Where<XRBContrDet.contractID, Equal<Current<XRBContrHdr.contractID>>>,
            OrderBy<Desc<XRBContrDet.genDate, Desc<XRBContrDet.lineNbr>>>>
            ContractDetails;

 

icon

Best answer by davidnavasardyan09 28 June 2023, 05:34

View original

4 replies

Userlevel 7
Badge +10

Hi @vibindas,

Please refer to the code snippet below. I have not tested it, but it might be helpful.

XRBContrDet newSchedule = (XRBContrDet)ContractDetails.Cache.CreateCopy((XRBContrDet)OldSchedule);
newSchedule.ContractID = NewContract.ContractID;
newSchedule.LineNbr = null;
newSchedule.NoteID = null;
newSchedule.CuryAmount = 0;
newSchedule.CuryPrePayBal = 0;
newSchedule.InvoiceCust = OldSchedule?.InvoiceCust;

newSchedule = ContractDetails.Insert(newSchedule);

Regards,

Sweta

Userlevel 3
Badge

@sweta68 , sorry if I phrased my question wrong. Issue is that though value is set for newSchedule.InvoiceCust = eg . “A”, while inserting newSchedule to View cache I can see the value is getting null thought it was set just before the insertion.

Userlevel 7
Badge +17

Hi @vibindas  Have you debugged and verified that this object newSchedule and observed any of the values are passing as NULL ?

Userlevel 5
Badge +1

Hi @vibindas. In your case, the error could be due to a couple of reasons:

  1. The OldSchedule object might be null: You are creating a copy of OldSchedule and assigning some values from OldSchedule to newSchedule. If OldSchedule is null at this point, you will get a null reference exception. You should make sure OldSchedule is not null before you try to use it.

  2. The ContractDetails View or its Cache might not be initialized: If you're trying to perform this operation in a graph extension, make sure the base graph is properly initialized. If this operation is being performed inside a row event handler, make sure the row is not null.

Try adding null checks to your code and also make sure you're executing this code in an appropriate context:

if(OldSchedule != null && ContractDetails != null)
{
XRBContrDet newSchedule = (XRBContrDet)ContractDetails.Cache.CreateCopy((XRBContrDet)OldSchedule);
newSchedule.ContractID = NewContract.ContractID;
newSchedule.LineNbr = null;
newSchedule.NoteID = null;
newSchedule.CuryAmount = 0;
newSchedule.CuryPrePayBal = 0;
newSchedule.InvoiceCust = OldSchedule.InvoiceCust;

newSchedule = ContractDetails.Insert(newSchedule);
}

If you're still encountering issues, you may want to check the stack trace of the exception to find out the exact line of code where the exception is being thrown. This could provide more insights into the source of the problem.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved