Skip to main content
Answer

Null value Exception when Inserting to Cache

  • June 23, 2023
  • 4 replies
  • 195 views

Forum|alt.badge.img

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;

 

Best answer by davidnavasardyan

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.

4 replies

Forum|alt.badge.img+9
  • Semi-Pro III
  • June 23, 2023

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


Forum|alt.badge.img
  • Author
  • June 26, 2023

@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.


Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • June 26, 2023

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


davidnavasardyan
Jr Varsity I
Forum|alt.badge.img+3

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.