Solved

Object reference not set to an instance of an object

  • 13 December 2023
  • 9 replies
  • 240 views

Userlevel 7
Badge +8

Hello all,

Does anyone have any idea why I am getting null exception (Subject Line error)?

 

if (fixedDemand != null && Base.FixedDemand != null && Base.FixedDemand.Any())
{
PXResult<POFixedDemand> matchingDemand = Base.FixedDemand.Select()
.Where(r => r.Record.RefNoteID == fixedDemand.RefNoteID &&
r.Record.FixedSource == fixedDemand.FixedSource &&
r.Record.SiteID == fixedDemand.SiteID &&
r.Record.PlanType == fixedDemand.PlanType &&
r.Record.PlanID == fixedDemand.PlanID &&
r.Record.RecordID == fixedDemand.RecordID)
.FirstOrDefault();
}

 

icon

Best answer by aaghaei 16 December 2023, 18:33

View original

9 replies

Userlevel 7
Badge +17

@aaghaei We could not able to know till debug and check the

values of the above code. However, I have added nullable types for the fields to handle the null exceptions. Can you please try with the below code and verify?

    if (fixedDemand != null && Base.FixedDemand != null && Base.FixedDemand.Any())
{
PXResult<POFixedDemand> matchingDemand = Base.FixedDemand.Select()
.Where(r => r.Record.RefNoteID == fixedDemand?.RefNoteID &&
r.Record.FixedSource == fixedDemand?.FixedSource &&
r.Record.SiteID == fixedDemand?.SiteID &&
r.Record.PlanType == fixedDemand?.PlanType &&
r.Record.PlanID == fixedDemand?.PlanID &&
r.Record.RecordID == fixedDemand?.RecordID)
.FirstOrDefault();
}

 

 

 

Badge +11

Writing code flexible enough to allow and handle nullable field values instead of constantly checking to make sure they don’t exist is an underrated practice.

Userlevel 7
Badge +8

@darylbowman thanks for the comment and @Naveen Boga thanks for the suggestion. the “fixedDemand” which is the current row of POFixedDemand is not null neither the Base.FixedDemand view it has elements. Any ways still I get the previous error. 

Userlevel 7
Badge +17

@aaghaei  Did you get a chance to debug each field value and verify?

Userlevel 7
Badge +8

Yes @Naveen Boga , but I can’t see the values inside the linq when tracing for r.Record.DataField. the fixedDemand and also the Base.FixedDemand they both have values.

Let me step back and expain why I have ended up here. I have a few custom fields and Field Selecting event in PO505000 that reads some values from SOLine, FSSODet and FSAppointmentDet depending on the record type and displays. SOLine and FSSODet required field can be accessed in one select so I get them without any issues. The Appointment lines do not have direct link so I get the Service Order line first then loop through the Appointment items to find the record I want. It works all fine but only when user tried to filter those fields gets an error that “Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space.”

This is the loop code that I am trying to convert it to a linq select as I provided in my post.

POFixedDemand matchingDemand;
FSSODetFSSODetSplit fsDetSplit = null;

foreach (PXResult<POFixedDemand> demand in Base.FixedDemand.Select())
{
matchingDemand = (POFixedDemand)demand;

if (matchingDemand.RefNoteID == fixedDemand.RefNoteID && matchingDemand.FixedSource == fixedDemand.FixedSource && matchingDemand.SiteID == fixedDemand.SiteID && matchingDemand.PlanType == fixedDemand.PlanType && matchingDemand.PlanID == fixedDemand.PlanID && matchingDemand.RecordID == fixedDemand.RecordID)
{
fsDetSplit = PXResult.Unwrap<FSSODetFSSODetSplit>(demand);
break;
}
}

In the above loop I only need the first record that matchs the condition as you can say from the code.

Userlevel 4
Badge +1

If I'm not mistaken FixedDemand view is used the PXFieldScope class to get/pupulate fileds values in the fixedDemand(). Most probabaly you are getting the error becasue your fields has not been predifined in the PXFieldScope constructor. You can do it by ovverriding the GetFixedDemandFieldScope() method. No RowSelecting events (e.g. from attributes) will be fired for the fields not fed into the FXFieldScope constructor.

Userlevel 7
Badge +8

Hi @vardan22 

Thank you for the response. Yes the graph uses the FieldScope but if you look into the method the whole POFixedDemand TABLE and some fields from other tables are included. So I do not have problem with reading and displaying values from the extension I have added to POFixedDemand. The fields get and display the values exactly I need in the UI. The problem surfaces only when I want to filter in my original code. 

Userlevel 7
Badge +8

@Naveen Boga @darylbowman @vardan22 Thank you all for the time you took to contribute to the possible solution. Below is what I ended up doing in case someone else comes across similar issue:

PXView query = new PXView(Base, false, Base.FixedDemand.View.BqlSelect);

query.WhereAnd<Where<POFixedDemand.refNoteID.IsEqual<@P.AsGuid>
.And<POFixedDemand.fixedSource.IsEqual<@P.AsString>>
.And<POFixedDemand.siteID.IsEqual<@P.AsInt>>
.And<POFixedDemand.planType.IsEqual<@P.AsString>>
.And<POFixedDemand.planID.IsEqual<@P.AsLong>>
.And<POFixedDemand.recordID.IsEqual<@P.AsInt>>>>();

var startRow = PXView.StartRow;
var totalRows = 0;
object[] parameters =
{
fixedDemand.RefNoteID,
fixedDemand.FixedSource,
fixedDemand.SiteID,
fixedDemand.PlanType,
fixedDemand.PlanID,
fixedDemand.RecordID
};

FSSODetFSSODetSplit fsDetSplit = null;
List<object> demandResults = query.Select(PXView.Currents, parameters, PXView.Searches, PXView.SortColumns, PXView.Descendings, PXView.Filters, ref startRow, PXView.MaximumRows, ref totalRows);

if (demandResults.Count() != 0)
{
fsDetSplit = PXResult.Unwrap<FSSODetFSSODetSplit>(demandResults[0]);
}

 

Userlevel 7
Badge

Thank you for sharing your solution with the community @aaghaei!

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