Part Tracker Customization error - Object reference not set to an instance of an object

I have narrowed down the line that is causing this error but I don’t know enough to fix it, hope someone can help
This is the code, The line between the messagebox’s is the current problem, I am sure the next line will have the same issue

public class Script
{
// ** Wizard Insert Location - Do Not Remove ‘Begin/End Wizard Added Module Level Variables’ Comments! **
// Begin Wizard Added Module Level Variables **

// End Wizard Added Module Level Variables **

// Add Custom Module Level Variables Here **
private EpiDataView edvPart;
//private EpiDataView edvPartWhse;

public void InitializeCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
	// Begin Wizard Added Variable Initialization

	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls

	this.btnPrintLabels.Click += new System.EventHandler(this.btnPrintLabels_Click);
	// End Wizard Added Custom Method Calls
	EpiDataView edvPart = ((EpiDataView)(oTrans.EpiDataViews["Part"]));
	//EpiDataView edvPartWhse = ((EpiDataView)(oTrans.EpiDataViews["PartWhse"]));
}

public void DestroyCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
	// Begin Wizard Added Object Disposal

	this.btnPrintLabels.Click -= new System.EventHandler(this.btnPrintLabels_Click);
	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	// End Custom Code Disposal
	this.edvPart = null;
	//this.edvPartWhse = null;
}

private void btnPrintLabels_Click(object sender, System.EventArgs args)
{
	// ** Place Event Handling Code Here **

MessageBox.Show(“btnPrintLabels_Click”);
string partNum = edvPart.dataView[edvPart.Row][“PartNum”].ToString();
MessageBox.Show(partNum);
string descr = edvPart.dataView[edvPart.Row][“PartDescription”].ToString();
//string location = edvPartWhse.dataView[edvPartWhse.Row][“PrimBinNum”].ToString();
string mtlAnalysisCode = edvPart.dataView[edvPart.Row][“MtlAnalysisCode”].ToString();

	string filelocation = string.Empty;
	string outputLine = string.Empty;
	EpiNumericEditor nbrLblQty = (EpiNumericEditor)csm.GetNativeControlReference("58bf5c04-f44b-4ae3-82b7-dba3489c85c6");
	decimal completedQty = System.Convert.ToDecimal(nbrLblQty.Value);
	if(completedQty > 0)
	{
		for (int a = 1; a <= completedQty; a++)
		{
			filelocation = @"\\server\folder\";
			int secs = System.Convert.ToInt32(DateTime.Now.TimeOfDay.TotalSeconds * 100);
			filelocation = filelocation + "P" + secs.ToString() + a.ToString() + ".LBL";
			outputLine = "^XA" + Environment.NewLine;
			if (completedQty > 1)
			{
				if (a < completedQty)
				{
					outputLine = outputLine + "^MMT" + Environment.NewLine;
				}
				else
				{
					outputLine = outputLine + "^MMC" + Environment.NewLine;
				}
			}
			else
			{
				outputLine = outputLine + "^MMC" + Environment.NewLine;
			}
			outputLine = outputLine + "^FO20,20^ADN,36,20^FD" +  partNum + "^FS" + Environment.NewLine;
			outputLine = outputLine + "^FO290,15^B3N,N,50,N,N^FD" + partNum + "^FS" + Environment.NewLine;
			outputLine = outputLine + "^FO20,75^ADN,28,10^FD" + descr + "^FS" + Environment.NewLine;
			outputLine = outputLine + "^FO20,115^ADN,28,10^FD" + mtlAnalysisCode  + "^FS" + Environment.NewLine;
			//outputLine = outputLine + "^FO20,165^ADN,36,20^FD" +  location + "^FS" + Environment.NewLine;
			//outputLine = outputLine + "^FO230,155^B3N,N,50,N,N^FD" + location + "^FS" + Environment.NewLine;

			outputLine = outputLine + "^XZ" + Environment.NewLine;
			using (var TCFile = new System.IO.StreamWriter(filelocation, true))
			{
				TCFile.WriteLine(outputLine);
				TCFile.Close();
			}
		}
	}
}

}

Hello Barry,

I can see that you declare a new EpiDataView edvPart = … in the InitializeCustomCode() Method. You don’t have to do that, just initialize it there:

// End Wizard Added Custom Method Calls
edvPart = ((EpiDataView)(oTrans.EpiDataViews["Part"]));
``
1 Like

Do you get the error when you first open the form? before any records are loaded?

I only get the error when I click the Print Labels button.

I’m defnintely not an expert when it comes to this… could it have something to do with the button click code is being written when the form is loaded and there is no part record yet selected?

1 Like

Thank you stoyanlevakov, that was the problem