Skip to main content
Question

I am Get erorr "Object reference not set to an instance of an object" when trying insert shipment


Forum|alt.badge.img
  • Freshman I
  • 22 replies

I am Get erorr "Object reference not set to an instance of an object" when trying insert shipment,

Why Like This ?
This the problem track

Please Help me Sense

 


Thanks you

6 replies

Forum|alt.badge.img
  • Jr Varsity III
  • 27 replies
  • March 25, 2025

Hi ​@Febri 

This error usually happens when the code tries to use an object that hasn't been initialized or it is null.

Can you share your customization or the code you're using? That will help in finding the exact issue.


Forum|alt.badge.img
  • Author
  • Freshman I
  • 22 replies
  • March 25, 2025

Thanks 
This is error code



and than the track like this



this is full code for metod rilise button 

public PXAction<ITMarking> Release;

[PXUIField(DisplayName = "RELEASE", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select, Visible = false)]
[PXButton(SpecialType = PXSpecialButtonType.Process, Connotation = PX.Data.WorkflowAPI.ActionConnotation.Success)]
public virtual IEnumerable release(PXAdapter adapter)
{
    PXTrace.WriteInformation("step 1");
    ITMarking rilise = MasterView.Current;

    if (rilise?.MarkingNbr != null && rilise.MarkingNbr != "" && rilise.MarkingNbr != "[DocumentID]" && rilise.MarkingNbr != "<NEW>")
    {
        SOShipmentEntry shipmentGraph = PXGraph.CreateInstance<SOShipmentEntry>();

        ShipementDua shipementLine = PXGraph.CreateInstance<ShipementDua>();

        var customerID = SelectFrom<SOOrder>
            .Where<SOOrder.orderNbr.IsEqual<P.AsString>>
            .View.Select(this, rilise.Sonbr)
            .TopFirst?.CustomerID;

        var customerLocationID = SelectFrom<SOOrder>
            .Where<SOOrder.orderNbr.IsEqual<P.AsString>>
            .View.Select(this, rilise.Sonbr)
            .TopFirst?.CustomerLocationID;

        PXTrace.WriteInformation("step 2");
        
        SOShipment shipment = new SOShipment
        {
            ShipDate = rilise.DocumentDate,
            ShipmentType = SOShipmentType.Issue,
            Operation = SOOperation.Issue,
            CustomerID = customerID,
            CustomerLocationID = customerLocationID,
            SiteID = rilise.Warehouse,
            Insurance = false,
            IsManualPackage = true,
        };
        shipment = shipmentGraph.Document.Insert(shipment);

        PXTrace.WriteInformation("step 3");
        if (shipment == null)
        {
            // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
            throw new PXException("Gagal membuat Shipment.");
        }

        shipmentGraph.Save.Press();

        PXTrace.WriteInformation("step 4");

        SOOrderShipment shipmentorder = new SOOrderShipment
        {
            OrderType = "SO",
            OrderNbr = rilise.Sonbr,
            ShipmentNbr = shipment.ShipmentNbr,
            Operation = SOOperation.Issue
        };

        shipmentorder = shipmentGraph.OrderList.Insert(shipmentorder);
        shipmentGraph.Save.Press();

        var detailsList = DetailsView.Select().RowCast<ITMarkingDetail>().ToList();
        if (detailsList == null || !detailsList.Any())
        {
            // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
            throw new PXException("Tidak ada data detail untuk diproses.");
        }

        PXTrace.WriteInformation("step 5");


        foreach (ITMarkingDetail details in detailsList)
        {
            //var lastShipLine = shipmentGraph.Transactions.Select().RowCast<SOShipLine>().FirstOrDefault(x => x.InventoryID == details.InventoryID);

            //if (lastShipLine == null)
            //{
            //    // Acuminator disable once PX1050 HardcodedStringInLocalizationMethod [Justification]
            //    throw new PXException($"Tidak ditemukan SOShipLine untuk InventoryID: {details.InventoryID}");
            //}
           

            SOShipLineSplit shipLineSplit = new SOShipLineSplit
            {
                ShipmentNbr = shipment.ShipmentNbr,
                SiteID = details.Warehouse,
                InventoryID = details.InventoryID,
                SubItemID = details.SubItem,
                Qty = details.ShipQty,
                UOM = details.Uom,
                LocationID = details.Location,
                LotSerialNbr = details.LotNbr,
                OrigOrderType="SO",
                OrigOrderNbr = rilise.Sonbr,
                LineNbr = 1,
                OrigLineNbr = 1,
                OrigSplitLineNbr=1,
                OrigPlanType="60",
                Operation = SOOperation.Issue,
                InvtMult = -1,
                LineType = "GI",
                IsIntercompany = false,
                PlanType = "61"
            };

            shipmentGraph.splits.Insert(shipLineSplit);
        }
        shipmentGraph.Save.Press();
       

        PXTrace.WriteInformation("step 6");

        foreach (var group in detailsList.GroupBy(d => new { d.InventoryID, d.SubItem, d.Warehouse, d.Location }))
        {

        

            SOShipLine dataInsertShip = new SOShipLine
            {
                ShipmentNbr = shipment.ShipmentNbr,
                SiteID = group.First().Warehouse,
                InventoryID = group.First().InventoryID,
                SubItemID = group.First().SubItem,
                Qty = group.First().ShipQty,
                UOM = group.First().Uom,
                LocationID = group.First().Location,
                LotSerialNbr = group.First().LotNbr,
                OrigOrderType = "SO",
                OrigOrderNbr = rilise.Sonbr,
                LineNbr = 1,
                OrigLineNbr = 1,
                OrigSplitLineNbr = 1,
                OrigPlanType = "60",
                Operation = SOOperation.Issue,
                InvtMult = -1,
                LineType = "GI",
                IsIntercompany = false,
                PlanType = "61"
            };
            
            shipmentGraph.Transactions.Insert(dataInsertShip);
            shipmentGraph.Save.Press();

        }

      

        PXTrace.WriteInformation("step 7");
        rilise.Status = "Released";
        rilise.ShipmentNbr = shipment.ShipmentNbr;
        MasterView.Cache.Update(rilise);
        MasterView.Cache.Persist(PXDBOperation.Update);

        //SOOrderEntry soOrderGraph = PXGraph.CreateInstance<SOOrderEntry>();

        //SOShipmentEntry soShipmentGraph = PXGraph.CreateInstance<SOShipmentEntry>();
        //soShipmentGraph.Clear();

        ////--------Create Shipment------------
        //DocumentList<SOShipment> created = new DocumentList<SOShipment>(soShipmentGraph);
        //soShipmentGraph.CreateShipment(soOrderGraph.Document.Current, siteId, soOrderGraph.Document.Current.OrderDate, false, SOOperation.Issue, created);

    }
    return adapter.Get();
}

Please Help me


Forum|alt.badge.img
  • Jr Varsity III
  • 27 replies
  • March 25, 2025

@Febri 

I have made some adjustments to your code to avoid nulls errors. Try with below code

foreach (var group in detailsList
             .Where(d => d.InventoryID != null && d.SubItem != null && d.Warehouse != null && d.Location != null)
             .GroupBy(d => new { d.InventoryID, d.SubItem, d.Warehouse, d.Location }))
{
    var firstItem = group.FirstOrDefault();

    if (firstItem == null)
    {
        PXTrace.WriteWarning("Skipping group due to null values.");
        continue;
    }

    SOShipLine dataInsertShip = new SOShipLine
    {
        ShipmentNbr = shipment.ShipmentNbr,
        SiteID = firstItem.Warehouse,
        InventoryID = firstItem.InventoryID,
        SubItemID = firstItem.SubItem,
        Qty = firstItem.ShipQty,
        UOM = firstItem.Uom,
        LocationID = firstItem.Location,
        LotSerialNbr = firstItem.LotNbr,
        OrigOrderType = "SO",
        OrigOrderNbr = rilise.Sonbr,
        LineNbr = 1,
        OrigLineNbr = 1,
        OrigSplitLineNbr = 1,
        OrigPlanType = "60",
        Operation = SOOperation.Issue,
        InvtMult = -1,
        LineType = "GI",
        IsIntercompany = false,
        PlanType = "61"
    };

    shipmentGraph.Transactions.Insert(dataInsertShip);
    shipmentGraph.Save.Press();
}

Thanks


Forum|alt.badge.img
  • Author
  • Freshman I
  • 22 replies
  • March 25, 2025

ok, i will try first.

i get same eorr bro


Forum|alt.badge.img
  • Author
  • Freshman I
  • 22 replies
  • March 25, 2025

Like This
 

 


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


Hi @Febri ! Acumatica often expects you to insert the parent record, mark it as Current, and then insert child records. Additionally, letting Acumatica handle line numbering (LineNbr) helps avoid mismatches between parent and child.

Below is a cleaned‐up version of your code that should work more reliably. The main difference is that I insert the SOShipment and set it to Document.Current right away, then insert the lines in shipmentGraph.Transactions, save, and only do additional split/lot adjustments afterward if needed.

Try out this code and see if it resolves the null reference error for you!

 

public PXAction<ITMarking> Release;
[PXUIField(DisplayName = "RELEASE", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select, Visible = false)]
[PXButton(SpecialType = PXSpecialButtonType.Process, Connotation = PX.Data.WorkflowAPI.ActionConnotation.Success)]
public virtual IEnumerable release(PXAdapter adapter)
{
    PXTrace.WriteInformation("Begin Release Action");

    ITMarking rilise = MasterView.Current;

    // Validate that we have a valid MarkingNbr
    if (rilise?.MarkingNbr != null 
        && rilise.MarkingNbr != "" 
        && rilise.MarkingNbr != "[DocumentID]" 
        && rilise.MarkingNbr != "<NEW>")
    {
        // Create graph for SOShipmentEntry
        SOShipmentEntry shipmentGraph = PXGraph.CreateInstance<SOShipmentEntry>();

        // Look up CustomerID and Location
        var soOrder = SelectFrom<SOOrder>
            .Where<SOOrder.orderNbr.IsEqual<P.AsString>>
            .View.Select(this, rilise.Sonbr).TopFirst;

        int? customerID = soOrder?.CustomerID;
        int? customerLocationID = soOrder?.CustomerLocationID;

        PXTrace.WriteInformation("Creating new Shipment record...");

        // Insert a new SOShipment record
        SOShipment shipment = new SOShipment
        {
            ShipDate          = rilise.DocumentDate,
            ShipmentType      = SOShipmentType.Issue,
            ....
        };
        shipment = shipmentGraph.Document.Insert(shipment);

        // Make sure the newly inserted shipment is set as the Current document
        shipmentGraph.Document.Current = shipment;

        // Save so the shipment is fully created in the database
        shipmentGraph.Save.Press();
        PXTrace.WriteInformation("Shipment record created: " + shipment.ShipmentNbr);


        SOOrderShipment shipmentOrder = new SOOrderShipment
        {
            OrderType   = "SO",
            OrderNbr    = rilise.Sonbr,
            ShipmentNbr = shipment.ShipmentNbr,
            Operation   = SOOperation.Issue
        };
        shipmentOrder = shipmentGraph.OrderList.Insert(shipmentOrder);
        shipmentGraph.Save.Press();

        PXTrace.WriteInformation("Adding shipment lines...");

        // Grab the details from your ITMarkingDetail lines
        var detailsList = DetailsView.Select().RowCast<ITMarkingDetail>().ToList();
        if (detailsList == null || !detailsList.Any())
        {
            throw new PXException("your error msg");
        }

        // Insert an SOShipLine for each item in details
        // Acumatica will handle the LineNbr automatically
        foreach (ITMarkingDetail detail in detailsList)
        {
            var shipLine = new SOShipLine
            {
                ShipmentNbr     = shipment.ShipmentNbr,
                SiteID          = detail.Warehouse,
                InventoryID     = detail.InventoryID,
                SubItemID       = detail.SubItem,
                ... 
            };
            shipmentGraph.Transactions.Insert(shipLine);
        }
        // Save after lines are inserted
        shipmentGraph.Save.Press();

        // If you need to manually modify splits (e.g., lot/serial management),
        // you can do so here by selecting the splits from shipmentGraph.splits
        // But in many cases, Acumatica auto-generates them for you.

        PXTrace.WriteInformation("All lines added; finalizing shipment...");

        // Update ITMarking record to indicate success
        rilise.Status = "Released";
        rilise.ShipmentNbr = shipment.ShipmentNbr;
        MasterView.Cache.Update(rilise);
        MasterView.Cache.Persist(PXDBOperation.Update);

        PXTrace.WriteInformation("Release action complete.");
    }

    return adapter.Get();
}

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings