Skip to main content

Getting this exception: Object reference not set to an instance of an object.  sometimes not regularly, during execution of this code :

 

K2Server.Open(_serverName, connectionString.ToString());

Worklist wl = default(Worklist);

WorklistCriteria wc = new WorklistCriteria();

wc.AddFilterField(WCField.SerialNumber, WCCompare.Equal, serialNumber);

wl = K2Server.OpenWorklist(wc);

if (wl.Count > 0)

{

WorklistItem wItem = default(WorklistItem);

wItem = wlw0];

var allocatedUser = wItem.AllocatedUser;

wi = K2Server.OpenWorklistItem(serialNumber, "ASP");

}

else

{

returnValue = "failed";

}

if (wi != null)

{

if (actionValue.Trim() == "Y")

wi.Actionsn"Successful"].Execute();

}

 

HI,

Try to take a log for the execution of the code. That's what I do normally to bedug the coed at run time.

 

1. Copy and paste the following method in code behind

2. Create two .txt file For tracing the output

3. Use the LogFile in your code as follows:

4. Check the logfile for the status of the checkpoint.

 

Also, check for the K2Server connection string. take a log for this too.

 

K2Server.Open(_serverName, connectionString.ToString());

Worklist wl = default(Worklist);

WorklistCriteria wc = new WorklistCriteria();

wc.AddFilterField(WCField.SerialNumber, WCCompare.Equal, serialNumber);

wl = K2Server.OpenWorklist(wc);

 

LogFile("CheckPoint-1 wl.count value: " + wl.count + Environment.NewLine);

if (wl.Count > 0)

{

LogFile("CheckPoint-2 inside count greater than 0." + Environment.NewLine);

WorklistItem wItem = default(WorklistItem);

wItem = wl 0];

var allocatedUser = wItem.AllocatedUser;

wi = K2Server.OpenWorklistItem(serialNumber, "ASP");

LogFile("CheckPoint-3." + Environment.NewLine);

}

else

{

LogFile("CheckPoint-4 return value assigned failed" + Environment.NewLine);

returnValue = "failed";

}

LogFile("CheckPoint-5 wi value: " + wi+Environment.NewLine);

 

if ( (wi != null) & (actionValue.Trim() == "Y"))

{

    LogFile("CheckPoint-6 " + wi+Environment.NewLine);

  wi.ActionsÂ"Successful"].Execute();

}

 

 

Here is the code:

 

private static void LogFile(string sMsg)

{

string sLogFile = "C:WorkflowLogK2Log.txt";

string sLogBackup = "C:WorkflowLogK2LogBak.txt";

FileInfo fi = new FileInfo(sLogFile);

try

{

if (File.Exists(sLogFile) && (fi.Length >= 100000))

{

File.Delete(sLogBackup);

File.Copy(sLogFile, sLogBackup);

File.Delete(sLogFile);

}

File.AppendAllText(sLogFile, sMsg);

}

catch (Exception exc)

{

string sErr = exc.Message + exc.StackTrace;

}

}

}

 


Thank you for providing this code however these exceptions do not occur in lower environments but only in PRODUCTION or LIVE environment.

So couldn’t find where these exceptions are occurring.

 

 

I have one query please find below code:

 

WorklistItem wItem = default(WorklistItem);

wItem = wl 0];

var allocatedUser = wItem.AllocatedUser;

wi = K2Server.OpenWorklistItem(serialNumber, "ASP");

if ( (wi != null) & (actionValue.Trim() == "Y"))

{

  wi.Actions<"Successful"].Execute();

}

 

Can wi.Actions be null ? Can this be the cause of these exceptions?

 

Request you to please provide any information regarding this.

 


Reply