BPM Substring issue


(Mike McWilliams) #1

I have the custom code below that I can’t get to work with Substring.
I can get the partNum and then display it in a message fine, but if I use partNum.Substring(0,5) I get an error at that point. If I hardode partNum and then use Substring the code works perfectly fine. Error is: ‘NullReferenceException’
Seems very odd.

var jobNum = ipJobNum;
var mill = "Mill 2";
var jobHead_xRow = (from JobHead_Row in Db.JobHead where JobHead_Row.JobNum == jobNum select JobHead_Row).FirstOrDefault();

string partNum = jobHead_xRow.PartNum;//Throws error when using Substring below
partNum = "F1016CR0I";//Works

string partSeg = partNum.Substring(0,5);
//PublishInfoMessage(partNum, Ice.Common.BusinessObjectMessageType.Information,Ice.Bpm.InfoMessageDisplayMode.Individual,"A","A");

var UD14_xRow = (from UD14_Row in Db.UD14 where UD14_Row.ShortChar06 == partSeg && UD14_Row.Key4 == mill select UD14_Row).FirstOrDefault();

var rate = UD14_xRow.Number07;
var filePath=@"C:\mill2.csv";
string combine = "NoCust" + "," + jobNum + "," + rate;

System.Text.StringBuilder sbOutput = new System.Text.StringBuilder();

System.IO.File.WriteAllText(filePath, sbOutput.ToString());

(Chris Conn) #2

Probably a case of where the string isnt 5 chars long… like if its blank. I notice you arent filtering your rows for only added\updated (which means there may be records you arent aware of)

var jobHead_xRow = (from JobHead_Row in Db.JobHead where JobHead_Row.JobNum == jobNum select JobHead_Row).FirstOrDefault();

You could do that, or simply do a length check of the string first

(Mike McWilliams) #3

The part number is always the same length 9 characters. I will double check the added or updated. This is in Labor. CheckEmployeeActivity so it is using the input paramater jobNum from start activity.

(Chris Conn) #4

Ah, I went back and re-read, I misunderstood. check that jobHead_Row is not null first. Is it possible this is called before the job is created? What method is this? Pre or Post?

(Haso Keric) #5

You can’t use Substring beyond the maximum amount of characters in C#.

"Haso".Substring(0, 4); // works
"Haso".Substring(0, 6); // Null Ref Exception

First, what you need is this (to be safe, incase you do have a partNum thats less than your expected length of 5)

string partNum = "PART123";
string partSeg = partNum.Length >= 5 ? partNum.Substring(0, 5) : partNum.Substring(0, partNum.Length);

Second, if your Job record is indeed null, you could also get that error.

(Mike McWilliams) #6

Post Update. I can write the jobnumber and the partNumber from the query to the text file no issues.

I added the length check and it still gives the error.
C# and strings always seem to give me headaches :slight_smile:

(Chris Conn) #7

What BO? Also, have you tried extracting the value to a string variable first, then performing your SubStr?

(Mike McWilliams) #8

I started from scratch and it is working now.
What was odd is that I had using for System.IO and System.Text, however it would say it was missing so I had to type it all in my code. After starting from scratch my using statements are working also.