ECOMtl added via Update give NullRrference Error

I am trying to add Materials to a Part in EWB. I have all the code working except for the materials.
FYI, I was able to get around this using UpdateExt, but it seems REALLY slow…
Here is my code:

foreach (Newtonsoft.Json.Linq.JObject mtl in jObj["EcoMtls"])
  {
    hEWB.GetNewECOMtl(ref dsEWB, groupID, partNum, defaultRev, "");
    Erp.Tablesets.ECOMtlRow mtlRow = dsEWB.ECOMtl.First(M => M.Added());
    mtlRow.MtlPartNum = (string)mtl["MtlPartNum"];
    hEWB.ChangeECOMtlMtlPartNum(ref dsEWB);
    hEWB.ChangeECOMtlRelatedOperation(relatedOp, ref dsEWB);
    mtlRow.QtyPer = (decimal)mtl["QtyPer"];
    hEWB.ChangeECOMtlQtyPer(ref dsEWB);
    hEWB.Update(ref dsEWB);
  }

The error I get is:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Epicor.Data.DBExpressionCompiler.GetObjectSetCache[TResult](EpiDataContext dataContext, Cache cacheSetting) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.System\Data\DBExpressionCompiler.cs:line 354
   at Epicor.Data.DBExpressionCompiler.InvokeSingle[TDataContext,TQuery,TResult](Expression expression, Cache currentCacheSetting, Boolean cacheQuery, TDataContext dataContext, Func`2 getDataCacheKey, Func`2 compileQuery, Func`3 executeQuery) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.System\Data\DBExpressionCompiler.cs:line 256
   at Epicor.Data.DBExpressionCompiler.<>c__DisplayClass58_0`6.<Compile>b__0(TContext context, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.System\Data\DBExpressionCompiler.Generated.cs:line 1169
   at Erp.Internal.BM.BMM10_GD.IsEcoRevCheckedOut(String company, String partNum, String revisionNum, String altMethod) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Internal\BM\EngWBLib\BMM10-GD.i.Queries.cs:line 1522
   at Erp.Internal.BM.BMM10_GD.FindEndlessLoop(String partNum, String revisionNum, String altMethod, Boolean isECOMtl) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Internal\BM\EngWBLib\BMM10-GD.i.cs:line 1047
   at Erp.Internal.BM.BMM10_GD.CrcCheck(String partNum, String revisionNum, String altMethod, Boolean isECOMtl) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Internal\BM\EngWBLib\BMM10-GD.i.cs:line 3394
   at Erp.Services.BO.EngWorkBenchSvc.ECOMtlAfterUpdate() in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Services\BO\EngWorkBench\EngWorkBench.cs:line 10537
   at Erp.Services.BO.EngWorkBenchSvc.OnRowEvent(DataTableEventType type, String tableName, IceRow row) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Services\BO\EngWorkBench\EngWorkBench.Designer.cs:line 801
   at Ice.Services.Trace.TablesetProfilingCollector.DoRowEventTrace(String tableName, String methodName, Int32 rowCount, Action action) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.Ice\Services\TablesetProfilingCollector.cs:line 138
   at Ice.TablesetBound`3.CreateRow(IceDataContext dataContext, Int32 tableNum, IIceTable table, IceRow newTablesetRow, TablesetProfilingCollector parentTraceCollector) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.Ice\Services\TablesetBound.cs:line 1185
   at Ice.TablesetBound`3.WriteTable(IceDataContext dataContext, Int32 tableIndex, IIceTable table, TablesetProfilingCollector parentTraceCollector) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.Ice\Services\TablesetBound.cs:line 971
   at Ice.TablesetBound`3.InnerUpdate[TUpdater](IceDataContext dataContext, TFullTableset tableset) in C:\_releases\ICE\ICE3.2.700.28\Source\Framework\Epicor.Ice\Services\TablesetBound.cs:line 880
   at Erp.Services.BO.EngWorkBenchSvc.Update(EngWorkBenchTableset& ds) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Services\BO\EngWorkBench\EngWorkBench.Designer.cs:line 4931
   at Erp.Services.BO.EngWorkBenchSvcFacade.Update(EngWorkBenchTableset& ds) in C:\_releases\ERP\ERP10.2.700.28\Source\Server\Services\BO\EngWorkBench\EngWorkBenchSvcFacade.cs:line 6686
   at EFx.PartGeneration.Implementation.CreatePartImpl.<>c__DisplayClass6_0.<Run>b__1(EngWorkBenchSvcContract hEWB)
   at Epicor.Functions.FunctionBase`3.CallService[TService](Action`1 action) in C:\_Releases\ICE\ICE3.2.700.0\Source\Server\Internal\Lib\Epicor.Functions.Core\FunctionBase.Plugins.cs:line 60
   at EFx.PartGeneration.Implementation.CreatePartImpl.Run()
   at Epicor.Functions.FunctionBase`3.Run(TInput input) in C:\_Releases\ICE\ICE3.2.700.0\Source\Server\Internal\Lib\Epicor.Functions.Core\FunctionBase.cs:line 75
   at Epicor.Functions.FunctionRestAdapter`2.Run(IFunctionRestHost host, JObject input) in C:\_Releases\ICE\ICE3.2.700.0\Source\Server\Internal\Lib\Epicor.Functions.Core\FunctionRestAdapter.cs:line 46
   at Epicor.RESTApi.Functions.Controllers.EpicorFunctionController.Post(Boolean production, String company, String library, String function, JObject data) in C:\_Releases\ICE\ICE3.2.700.0\Source\Server\Internal\RESTApi.Plugins\Epicor.RESTApi.EFxPlugin\Controllers\EpicorFunctionController.cs:line 69
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

I’d comment out the whole block in the foreach, to see if it is balking at jObj["EcoMtls"]

Then uncomment the first line in the block hEWB.GetNewECOMtl(re... to see what happens. And then just keep adding one line at a time to see which line is throwing the error.

It errors at the line for the update.

hEWB.Update(ref dsEWB);

C# really isn’t my thing … But does the following apply?

Modifiers Are Not Allowed on All Methods

It’s important to note that in, out, and ref cannot be used in methods with the async modifier. You can use them in synchronous methods that return a task, though.

Quick test would to try it without the ref modifier.

Edit

Nevermind… I see that’s the way it’s normally used.
I assume dsEWB is declared and initialized prior to the for each loop. Perhaps it needs to be declared or reinitialized at the beginning of each for loop.

I was able to get it working with UpdateExt, but it takes about 2 minutes to process 75 materials. Seems slow…

I am facing the exact same issue, did you ever figure out what caused the error? And how do you transform a EngWorkBenchTableset into a UpdExtEngWorkBenchTableset for use with UpdateExt?

foreach (Newtonsoft.Json.Linq.JObject mtl in jObj["EcoMtls"])
  {
    if (dsEWB != null && dsEWB.ECOMtl != null && dsEWB.ECOMtl.Count > 0)
    {
        hEWB.GetNewECOMtl(ref dsEWB, groupID, partNum, defaultRev, "");
        var mtlRow = dsEWB.ECOMtl.FirstOrDefault(M => M.Added());
        if (mtlRow != null)
        {
            mtlRow.MtlPartNum = (string)mtl["MtlPartNum"];
            hEWB.ChangeECOMtlMtlPartNum(ref dsEWB);
            hEWB.ChangeECOMtlRelatedOperation(relatedOp, ref dsEWB);
            mtlRow.QtyPer = (decimal)mtl["QtyPer"];
            hEWB.ChangeECOMtlQtyPer(ref dsEWB);
            hEWB.Update(ref dsEWB);
        }
    }
  }

This version first checks if dsEWB is not null, and then checks if dsEWB.ECOMtl is not null or empty before trying to access any properties or methods on it. Additionally, it also checks if there is a row in dsEWB.ECOMtl that satisfies the condition M.Added() and add an additional null check before trying to access this variable. Please note that this is just a sample solution based on the information you’ve provided. If your problem still persists, it is recommended to check the value of the variables and the context of your code to see if there are any other issues.

Thanks, but doesn’t help unfortunately…

What does the trace say? Does it use UpdateExt or Update or both…

Well, I don’t know whether to laugh or cry… Followed this suggestion: Building a part MOM on the EWB BO -- CheckIn issues - #3 by jdtrent .

Works fine if I instantiate a new BO… Probably not a bug right? :wink: