Skip to main content
Answer

Object reference not set to an instance of an object. Error in custom action when going to create a data recod

  • September 30, 2024
  • 7 replies
  • 214 views

Forum|alt.badge.img+2

I’m Implementing a custom Material request note (MRN) screen. Now I need to add a custom button to Production orders screen which create a MRN based on that production order. currently it gives the Object reference not set to an instance of an object. error. I have added few if conditions to check the null values, but it did not hit any if condition. Following is my Action code
 

        public PXAction<AMProdItem> CreateMRN;
[PXButton(DisplayOnMainToolbar = true, CommitChanges = true)]
[PXUIField(DisplayName = "Create MRN", Enabled = true)]
protected virtual IEnumerable createMRN(PXAdapter adapter)
{
// Populate a local inventoryItemList variable.
List<AMProdMatl> inventoryItemList = new List<AMProdMatl>();
foreach (AMProdMatl inventoryItem in adapter.Get<AMProdMatl>())
{
inventoryItemList.Add(inventoryItem);
}
if(inventoryItemList.Count == 0)
{
string msg = "MRN - Item list is empty";
throw new PXException(msg);
}
// Trigger the Save action to save changes in the database.
//Base.Actions.PressSave();
var productionOrder = Base.ProdItemRecords.Current;
if (productionOrder == null)
{
string msg = "MRN - production order is null";
throw new PXException(msg);
}
/*Execute ValidatePrices method asynchronously
using PXLongOperation.StartOperation*/
PXLongOperation.StartOperation(this,
() => CreateMRNDoc(productionOrder.ProdOrdID, inventoryItemList)
);
// Return the local inventoryItemList variable.
return inventoryItemList;
}

private static void CreateMRNDoc(string orderId, List<AMProdMatl> itemList )
{
using (var ts = new PXTransactionScope())
{

var mRNEntry = PXGraph.CreateInstance<MRMaterialRequestNoteEntry>();
// Initialize the summary of the invoice.
var doc = new MRMaterialRequest()
{
DocType = MRNDocType.ProductionOrder,
DocDate = DateTime.Now,
DepartmentID = "1",
Status = MRNStatus.Open,
Description = orderId
};

doc = mRNEntry.MaterialRequestView.Insert(doc);
doc = mRNEntry.MaterialRequestView.Update(doc);
mRNEntry.Actions.PressSave();
mRNEntry.MaterialRequestView.Current = doc;

if( doc == null )
{
string msg = "MRN - Material request note is null";
throw new PXException(msg);
}

foreach (AMProdMatl item in itemList)
{
if (item.InventoryID == null)
{
string msg = "inventory id is null";
throw new PXException(msg);
};
var itemDoc = new MRInventoryItems()
{
//Mrrefnbr = "0000006",
InventoryID = item.InventoryID,
OrderQty = item.TotalQtyRequired,
SourceOrderNbr = orderId
};

if (itemDoc == null)
{
string msg = "MRN - Material request note item document is null";
throw new PXException(msg);
}
itemDoc = mRNEntry.MaterialDetailsView.Insert(itemDoc);
mRNEntry.MaterialDetailsView.Update(itemDoc);
}

mRNEntry.Actions.PressSave();

ts.Complete();
}
}

when commented the foreach loop which create the MRInventoryItems record list, rest of the code work fine. and create a new MRN record without the inventory items.

Best answer by hdussa

Hello @PDharmasena10 ,

 

The MoveNext error could be because you are returning NULL at the end of the action. I would change it to adapter.Get(); as a standard practice.

7 replies

DipakNilkanth
Pro III
Forum|alt.badge.img+13

Hi @PDharmasena10,

Have you debugged your code? Were you able to identify which line is causing the error?

It’s possible that the itemList object is returning null, which could be causing the object reference error when trying to fetch the InventoryDI from the itemList object.

Have you checked the trace after getting the error? This should give you more insight into where exactly the issue is arising.

Hope, it helps!


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • September 30, 2024

Hi @Dipak Nilkanth, As you told it happens due to the itemList is null. So, I use the relevant view of the base graph instead of the adapter to fetch the recodes. It works as expected but gives the following warning massage. when click Ok button it creates the new MRN note as exected.

 

Following is my updated code.

        public PXAction<AMProdItem> CreateMRN;
[PXButton(DisplayOnMainToolbar = true, CommitChanges = true)]
[PXUIField(DisplayName = "Create MRN", Enabled = true)]
protected virtual IEnumerable createMRN(PXAdapter adapter)
{

var inventoryItemList = Base.ProdMatlRecords.Select();

if (inventoryItemList.Count == 0)
{
string msg = "MRN - Item list is empty";
throw new PXException(msg);
}
var productionOrder = Base.ProdItemRecords.Current;
if (productionOrder == null)
{
string msg = "MRN - production order is null";
throw new PXException(msg);
}
PXLongOperation.StartOperation(this,
() => CreateMRNDoc(productionOrder.ProdOrdID, inventoryItemList)
);
return null;
}

private static void CreateMRNDoc(string orderId, PXResultset<AMProdMatl> itemList )
{
using (var ts = new PXTransactionScope())
{
var mRNEntry = PXGraph.CreateInstance<MRMaterialRequestNoteEntry>();
var doc = new MRMaterialRequest()
{
DocType = MRNDocType.ProductionOrder,
DocDate = DateTime.Now,
DepartmentID = "1",
Status = MRNStatus.Open,
Description = orderId
};
doc = mRNEntry.MaterialRequestView.Insert(doc);
doc = mRNEntry.MaterialRequestView.Update(doc);
mRNEntry.Actions.PressSave();
mRNEntry.MaterialRequestView.Current = doc;
if( doc == null )
{
string msg = "MRN - Material request note is null";
throw new PXException(msg);
}
foreach(AMProdMatl item in itemList )
{
var itemDoc = new MRInventoryItems()
{
InventoryID = item.InventoryID,
OrderQty = item.TotalQtyRequired,
SourceOrderNbr = orderId
};
if (itemDoc == null)
{
string msg = "MRN - Material request note item document is null";
throw new PXException(msg);
}
itemDoc = mRNEntry.MaterialDetailsView.Insert(itemDoc);
mRNEntry.MaterialDetailsView.Update(itemDoc);
}
mRNEntry.Actions.PressSave();
ts.Complete();
}
}

 


DipakNilkanth
Pro III
Forum|alt.badge.img+13

Hi @PDharmasena10 ,

Please check the trace as well.


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • September 30, 2024

Hi @Dipak Nilkanth  I check it. it still complains about a null reference. but all the functions work as expected.

 


darylbowman
Captain II
Forum|alt.badge.img+15

Try this:

var inventoryItemList = Base.ProdMatlRecords.Select();

if (inventoryItemList is null || inventoryItemList.Count == 0)
{
string msg = "MRN - Item list is empty";
throw new PXException(msg);
}

 


hdussa
Jr Varsity I
Forum|alt.badge.img+1
  • Jr Varsity I
  • Answer
  • September 30, 2024

Hello @PDharmasena10 ,

 

The MoveNext error could be because you are returning NULL at the end of the action. I would change it to adapter.Get(); as a standard practice.


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • September 30, 2024

Hi @Dipak Nilkanth , @Dipak Nilkanth , @darylbowman , Thanks all for your support. First issue happened due to the itemList been null, and second issue is due to returning null from the action method