Object reference not set to an instance of an object.

I am working on a form which will print to a label. when i hit my print button I get the message “Object reference not set to an instance of an object.”

Below is my code.

[FormControlEventHandler(formControlStr(MJMNeTubingLabel, MJMPrintTubingLabel), FormControlEventType::Clicked)]
public static void MJMPrintTubingLabel_OnClicked(FormControl sender, FormControlEventArgs e)
{
FormRun formRun = sender.formRun();
FormDataSource formDataSource = sender.formRun().dataSource(“SalesTable”);

UserInfo userInfo;
MJMLabelFormatSupport UsersLabel;
MJM_ScanFolder ScanFolder;

// this needs to come in as variable
str descript;
//On the TubingRound Label
FormComboBoxControl formTubingLabelFormat = FormRun.design(0).controlName(“MJMTubingLabel_TubingLabelFormat”);
FormStringControl formSalesId = formRun.design(0).controlName(“MJMFinShipLabel_SalesId”);
FormStringControl formCustomerRef = formRun.design(0).controlName(“MJMFinShipLabel_CustomerRef”);//OrderNum
FormStringControl formCageNum = formRun.design(0).controlName(“MJMFinShipLabel_CageNum”);
FormStringControl formSpoolPartNumber = formRun.design(0).controlName(“MJMTubingLabel_MJMSpoolPartNumber”);//Manual enter
FormStringControl formExternalItemId = formRun.design(0).controlName(“MJMFinShipLabel_ExternalItemId”);//PartNum
FormStringControl formLot = FormRun.design(0).controlName(“MJMFinShipLabel_Lot”);
FormStringControl formAWGSize = FormRun.design(0).controlName(“MJMFinShipLabel_AWGSize”);
FormStringControl formSalesUnit = FormRun.design(0).controlName(“MJMFinShipLabel_SalesUnit”);
FormStringControl formReelNum = FormRun.design(0).controlName(“MJMFinShipLabel_ReelNum”);
FormRealControl formNumPieces = FormRun.design(0).controlName(“MJMFinShipLabel_NumPieces”);
FormRealControl formLength = FormRun.design(0).controlName(“MJMFinShipLabel_Length”);
FormRealControl formTopPc = FormRun.design(0).controlName(“MJMFinShipLabel_TopPc”);
FormDateControl formMFGDate = FormRun.design(0).controlName(“MJMFinShipLabel_MFGDate”);
FormRealControl formGross = FormRun.design(0).controlName(“MJMFinShipLabel_Gross”);
FormRealControl formTare = FormRun.design(0).controlName(“MJMFinShipLabel_Tare”);
FormRealControl formNet = FormRun.design(0).controlName(“MJMFinShipLabel_Net”);
FormIntControl formNumberOfLabels = FormRun.design(0).controlName(“MJMFinShipLabel_NumberOfLabels”);

//On the TubingSquare Label
FormStringControl formItemNo = formRun.design(0).controlName(“MJMTubingLabel_MJMItemNo”);
FormStringControl formOrderNumber = formRun.design(0).controlName(“MJMTubingLabel_MJMOrderNumber”);
FormStringControl formPartNumber = formRun.design(0).controlName(“MJMTubingLabel_MJMPartNumber”);
FormStringControl formMfgPartNum = formRun.design(0).controlName(“MJMTubingLabel_MJMMFGPartNo”);//Manual enter
FormStringControl formBulkWorkOrder = formRun.design(0).controlName(“MJMTubingLabel_MJMBulkWorkOrder”);
FormStringControl formMfgLotNum = formRun.design(0).controlName(“MJMTubingLabel_MJMMFGLotNum”);//Manual enter
FormStringControl formRev = FormRun.design(0).controlName(“MJMTubingLabel_MJMRevision”);
FormStringControl formDescription = FormRun.design(0).controlName(“MJMTubingLabel_MJMTubingDescription”);

//On the TubingSquare Label
FormStringControl formOutsideDmter = formRun.design(0).controlName(“MJMTubingLabel_MJMOutsideDmter”);//Manual enter

//FormRealControl formLineNum = FormRun.design(0).controlName(“MJMFinShipLabel_LineNum”);
//FormStringControl formItemId = formRun.design(0).controlName(“MJMFinShipLabel_ItemId”);//MfgPartNum

date MFGDate = str2Date(formMFGDate.valueStr(),321);
Str tubingLabel = formTubingLabelFormat.valueStr();

//get the user mane to find format
select * from userInfo
where userInfo.name == xUserInfo::find().name;

//info(strfmt("%1 %2", xUserInfo::find().name, userInfo.id ));

// this is create a unque key for the format file
System.DateTime dateTime = System.DateTime::get_UtcNow();
str utcTimeAsStr = dateTime.ToString(‘yyyyMdHHmmss’);

// find user template and printer
if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
{
select * from userslabel
where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == “NTRL”;
}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))
{
select * from userslabel
where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == “NTSL”;
}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))
{
select * from userslabel
where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == “NTCL”;
}

// get location to drop files
select * from ScanFolder
where ScanFolder.MJM_ScanFolder_Name == “Bartender”;

// now we create and write the file
System.IO.StreamWriter sw;
InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);

perm.assert();
// file name and location
sw = new System.IO.StreamWriter(strRTrim(ScanFolder.MJM_ScanFolder_Path) + userslabel.User + utcTimeAsStr + “.dat”);
// header
sw.WriteLine("%BTW% /f=" + UsersLabel.LabelTemplate + " " /d="%Tigger File Name%" /PRN=" " + UsersLabel.LabelPrinter + “” /r=3 /p /dd /c=" + formNumberOfLabels.valueStr()+ “”");
sw.WriteLine("%END%");
//variable names
if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
{
sw.WriteLine(“AwgSize, Cage, CustomerPN, SpoolPartNumber, CustomerPo, Lot, Gross, Tare, Net, UOM, ReelNum, NoPcs, Length, TopPiece, SalesOrder, Mfgdate, AwgSizeDesc, CageDesc, CustomerPNDesc, SpoolPartNumberDesc, CustomerPoDesc, LotDesc, GrossDesc, TareDesc, NetDesc, UOMDesc, ReelNumDesc, NoPcsDesc, LengthDesc, TopPieceDesc, SalesOrderDesc, MfgdateDesc”);
}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))// Tubing Square Label
{
sw.WriteLine(“OrderNo, ItemNo, PartNum, Rev, MfgLotNo, MfgPartNo, SalesOrder, Description, BulkWorkOrder, Quantity, OrderNoDesc, ItemNoDesc, PartNumDesc, RevDesc, MfgLotDesc, MfgPartDesc, SalesOrderDesc, DescriptionDesc, BulkWorkOrderDesc, QuantityDesc”);
}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))//Cable Square Label
{
sw.WriteLine(“OrderNo, ItemNo, PartNum, Rev, MfgLotNo, MfgPartNo, SalesOrder, Description, MfgDate, OutsideDmter, Quantity, OrderNoDesc, ItemNoDesc, PartNumDesc, RevDesc, MfgLotDesc, MfgPartDesc, SalesOrderDesc, DescriptionDesc, MfgDateDesc, OutsideDmterDesc, QuantityDesc”);
}

Descript = System.Text.RegularExpressions.Regex::Replace(formDescription.valueStr(), “”", “”);
if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
{
sw.WriteLine(
“”" + formAWGSize.valueStr() + “”,"" +
formCageNum.valueStr() + “”,"" +
formExternalItemId.valueStr() + “”,"" +
formSpoolPartNumber.valueStr() + “”,"" +
formCustomerRef.valueStr() + “”,"" +
formLot.valueStr() + “”,"" +
formGross.valueStr() + “”,"" +
formTare.valueStr() + “”,"" +
formNet.valueStr() + “”,"" +
formSalesUnit.valueStr() + “”,"" +
formReelNum.valueStr() + “”,"" +
formNumPieces.valueStr() + “”,"" +
formLength.valueStr() + “”,"" +
formTopPc.valueStr() + “”,"" +
formSalesId.valueStr() + “”,"" +
strFmt("%1/%2/%3",mthOfYr(MfgDate),dayOfMth(MfgDate),Year(MfgDate)) + “”,"" +

“AWG Size:” + “”,"" +
“Cage #:” + “”,"" +
“Customer P/N:” + “”,"" +
“Part #:” + “”,"" +
“Customer PO #:” + “”,"" +
“Lot #:” + “”,"" +
“Gross:” + “”,"" +
“Tare:” + “”,"" +
“NET:” + “”,"" +
“Unit:” + “”,"" +
“Reel #:” + “”,"" +
“No Pcs:” + “”,"" +
“Length:” + “”,"" +
“Top Piece:” + “”,"" +
“Sales Order:” + “”,"" +
“Mfg Date:” + “”,"");

sw.Flush();
sw.Close();
sw.Dispose();

}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))
{
sw.WriteLine(
“”" + formOrderNumber.valueStr() + “”,"" +
formItemNo.valueStr() + “”,"" +
formPartNumber.ValueStr() + “”,"" +
formRev.valueStr() + “”,"" +
formMfgLotNum.valueStr() + “”,"" +
formMfgPartNum.valueStr() + “”,"" +
formSalesId.valueStr() + “”,"" +
descript + “”,"" +
formBulkWorkOrder.valueStr() + “”,"" +
formLength.valueStr() + “”,"" +

“Order No:” + “”,"" +
“Item No:” + “”,"" +
“Part No:” + “”,"" +
“Rev:” + “”,"" +
“MFG Lot No:” + “”,"" +
“MFG Part No:” + “”,"" +
“Sales Order:” + “”,"" +
“Description:” + “”,"" +
“Bulk Work Order:” + “”,"" +
“Quantity:” + “”,"");

sw.Flush();
sw.Close();
sw.Dispose();

}
else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))
{
sw.WriteLine(
“”" + formOrderNumber.valueStr() + “”,"" +
formItemNo.valueStr() + “”,"" +
formPartNumber.ValueStr() + “”,"" +
formRev.valueStr() + “”,"" +
formMfgLotNum.valueStr() + “”,"" +
formMfgPartNum.valueStr() + “”,"" +
formSalesId.valueStr() + “”,"" +
descript + “”,"" +
formMFGDate.valueStr() + “”,"" +
formOutsideDmter.valueStr() + “”,"" +
formLength.valueStr() + “”,"" +

“Order No:” + “”,"" +
“Item No:” + “”,"" +
“Part No:” + “”,"" +
“Rev:” + “”,"" +
“MFG Lot No:” + “”,"" +
“MFG Part No:” + “”,"" +
“Sales Order:” + “”,"" +
“Description:” + “”,"" +
“MFG Date:” + “”,"" +
“O.D.:” + “”,"" +
“Quantity:” + “”,"");

sw.Flush();
sw.Close();
sw.Dispose();
}

}

}

have u debug the code written by you , same where - form object might not be initialized

Which line of code is throwing the error?

By the way, please use Insert > Insert code to paste source code; it’ll make it easier to read.

[FormControlEventHandler(formControlStr(MJMNeTubingLabel, MJMPrintTubingLabel), FormControlEventType::Clicked)]
    public static void MJMPrintTubingLabel_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormRun             formRun = sender.formRun();
        FormDataSource      formDataSource = sender.formRun().dataSource("SalesTable");
       
        UserInfo            userInfo;
        MJMLabelFormatSupport       UsersLabel;
        MJM_ScanFolder      ScanFolder;

        // this needs to come in as variable
        str descript;
        //On the TubingRound Label
        FormComboBoxControl         formTubingLabelFormat = FormRun.design(0).controlName("MJMTubingLabel_TubingLabelFormat");
        FormStringControl           formSalesId = formRun.design(0).controlName("MJMFinShipLabel_SalesId");
        FormStringControl           formCustomerRef = formRun.design(0).controlName("MJMFinShipLabel_CustomerRef");//OrderNum
        FormStringControl           formCageNum = formRun.design(0).controlName("MJMFinShipLabel_CageNum");
        FormStringControl           formSpoolPartNumber = formRun.design(0).controlName("MJMTubingLabel_MJMSpoolPartNumber");//Manual enter
        FormStringControl           formExternalItemId = formRun.design(0).controlName("MJMFinShipLabel_ExternalItemId");//PartNum
        FormStringControl           formLot = FormRun.design(0).controlName("MJMFinShipLabel_Lot");
        FormStringControl           formAWGSize = FormRun.design(0).controlName("MJMFinShipLabel_AWGSize");
        FormStringControl           formSalesUnit = FormRun.design(0).controlName("MJMFinShipLabel_SalesUnit");
        FormStringControl           formReelNum = FormRun.design(0).controlName("MJMFinShipLabel_ReelNum");
        FormRealControl             formNumPieces = FormRun.design(0).controlName("MJMFinShipLabel_NumPieces");
        FormRealControl             formLength = FormRun.design(0).controlName("MJMFinShipLabel_Length");
        FormRealControl             formTopPc = FormRun.design(0).controlName("MJMFinShipLabel_TopPc");
        FormDateControl             formMFGDate = FormRun.design(0).controlName("MJMFinShipLabel_MFGDate");
        FormRealControl             formGross = FormRun.design(0).controlName("MJMFinShipLabel_Gross");
        FormRealControl             formTare = FormRun.design(0).controlName("MJMFinShipLabel_Tare");
        FormRealControl             formNet = FormRun.design(0).controlName("MJMFinShipLabel_Net");
        FormIntControl              formNumberOfLabels = FormRun.design(0).controlName("MJMFinShipLabel_NumberOfLabels");
        
        //On the TubingSquare Label
        FormStringControl           formItemNo = formRun.design(0).controlName("MJMTubingLabel_MJMItemNo");
        FormStringControl           formOrderNumber = formRun.design(0).controlName("MJMTubingLabel_MJMOrderNumber");
        FormStringControl           formPartNumber = formRun.design(0).controlName("MJMTubingLabel_MJMPartNumber");
        FormStringControl           formMfgPartNum = formRun.design(0).controlName("MJMTubingLabel_MJMMFGPartNo");//Manual enter
        FormStringControl           formBulkWorkOrder = formRun.design(0).controlName("MJMTubingLabel_MJMBulkWorkOrder");
        FormStringControl           formMfgLotNum = formRun.design(0).controlName("MJMTubingLabel_MJMMFGLotNum");//Manual enter
        FormStringControl           formRev = FormRun.design(0).controlName("MJMTubingLabel_MJMRevision");
        FormStringControl           formDescription = FormRun.design(0).controlName("MJMTubingLabel_MJMTubingDescription");
        
        //On the TubingSquare Label
        FormStringControl           formOutsideDmter = formRun.design(0).controlName("MJMTubingLabel_MJMOutsideDmter");//Manual enter
        
        
        
        
        //FormRealControl             formLineNum = FormRun.design(0).controlName("MJMFinShipLabel_LineNum");
        //FormStringControl           formItemId = formRun.design(0).controlName("MJMFinShipLabel_ItemId");//MfgPartNum
        

        date MFGDate = str2Date(formMFGDate.valueStr(),321);
        Str tubingLabel = formTubingLabelFormat.valueStr();
       

        //get the user mane to find format
        select * from userInfo
            where userInfo.name == xUserInfo::find().name;

        //info(strfmt("%1 %2", xUserInfo::find().name, userInfo.id ));

        // this is create a unque key for the format file
        System.DateTime dateTime = System.DateTime::get_UtcNow();
        str utcTimeAsStr = dateTime.ToString('yyyyMdHHmmss');

        // find user template and printer
        if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
        {
            select * from userslabel
        where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == "NTRL";
        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))
        {
            select * from userslabel
        where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == "NTSL";
        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))
        {
            select * from userslabel
        where UsersLabel.User == userInfo.id && UsersLabel.LabelFormat == "NTCL";
        }

        // get location to drop files
        select * from ScanFolder
            where ScanFolder.MJM_ScanFolder_Name == "Bartender";



        // now we create and write the file
        System.IO.StreamWriter sw;
        InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);

        perm.assert();
        // file name and location
        sw = new System.IO.StreamWriter(strRTrim(ScanFolder.MJM_ScanFolder_Path) + userslabel.User + utcTimeAsStr + ".dat");
        // header
        sw.WriteLine("%BTW% /f=" + UsersLabel.LabelTemplate +  " \" /d=\"%Tigger File Name%\" /PRN=\" " + UsersLabel.LabelPrinter + "\"  /r=3 /p /dd /c=" + formNumberOfLabels.valueStr()+ "\"");
        sw.WriteLine("%END%");
        //variable names
        if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
        {
            sw.WriteLine("AwgSize, Cage, CustomerPN, SpoolPartNumber, CustomerPo, Lot, Gross, Tare, Net, UOM, ReelNum, NoPcs, Length, TopPiece, SalesOrder, Mfgdate, AwgSizeDesc, CageDesc, CustomerPNDesc, SpoolPartNumberDesc, CustomerPoDesc, LotDesc, GrossDesc, TareDesc, NetDesc, UOMDesc, ReelNumDesc, NoPcsDesc, LengthDesc, TopPieceDesc, SalesOrderDesc, MfgdateDesc");
        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))// Tubing Square Label
        {
            sw.WriteLine("OrderNo, ItemNo, PartNum, Rev, MfgLotNo, MfgPartNo, SalesOrder, Description, BulkWorkOrder, Quantity, OrderNoDesc, ItemNoDesc, PartNumDesc, RevDesc, MfgLotDesc, MfgPartDesc, SalesOrderDesc, DescriptionDesc, BulkWorkOrderDesc, QuantityDesc");
        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))//Cable Square Label
        {
            sw.WriteLine("OrderNo, ItemNo, PartNum, Rev, MfgLotNo, MfgPartNo, SalesOrder, Description, MfgDate, OutsideDmter, Quantity, OrderNoDesc, ItemNoDesc, PartNumDesc, RevDesc, MfgLotDesc, MfgPartDesc, SalesOrderDesc, DescriptionDesc, MfgDateDesc, OutsideDmterDesc, QuantityDesc");
        }
        
        Descript =  System.Text.RegularExpressions.Regex::Replace(formDescription.valueStr(), "\"", "");
        if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingRoundLabel))
        {
        sw.WriteLine(
                "\"" + formAWGSize.valueStr() + "\",\"" +
                formCageNum.valueStr() + "\",\"" +
                formExternalItemId.valueStr() + "\",\"" +
                formSpoolPartNumber.valueStr() + "\",\"" +
                formCustomerRef.valueStr() + "\",\"" +
                formLot.valueStr() + "\",\"" +
                formGross.valueStr() + "\",\"" +
                formTare.valueStr() + "\",\"" +
                formNet.valueStr() + "\",\"" +
                formSalesUnit.valueStr() + "\",\"" +
                formReelNum.valueStr() + "\",\"" +
                formNumPieces.valueStr() + "\",\"" +
                formLength.valueStr() + "\",\"" +
                formTopPc.valueStr() + "\",\"" +
                formSalesId.valueStr() + "\",\"" +
                strFmt("%1/%2/%3",mthOfYr(MfgDate),dayOfMth(MfgDate),Year(MfgDate)) + "\",\"" +
                
                "AWG Size:" + "\",\"" +
                "Cage #:" + "\",\"" +
                "Customer P/N:" + "\",\"" +
                "Part #:" + "\",\"" +
                "Customer PO #:" + "\",\"" +
                "Lot #:" + "\",\"" +
                "Gross:" + "\",\"" +
                "Tare:" + "\",\"" +
                "NET:" + "\",\"" +
                "Unit:" + "\",\"" +
                "Reel #:" + "\",\"" +
                "No Pcs:" + "\",\"" +
                "Length:" + "\",\"" +
                "Top Piece:" + "\",\"" +
                "Sales Order:" + "\",\"" +
                "Mfg Date:" + "\",\"");

                sw.Flush();
                sw.Close();
                sw.Dispose();

        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::TubingSquareLabel))
        {
        sw.WriteLine(
            "\"" + formOrderNumber.valueStr() + "\",\"" +
                formItemNo.valueStr() + "\",\"" +
                formPartNumber.ValueStr() + "\",\"" +
                formRev.valueStr() + "\",\"" +
                formMfgLotNum.valueStr() + "\",\"" +
                formMfgPartNum.valueStr() + "\",\"" +
                formSalesId.valueStr() + "\",\"" +
                descript + "\",\"" +
                formBulkWorkOrder.valueStr() + "\",\"" +
                formLength.valueStr() + "\",\"" +
               

                "Order No:" + "\",\"" +
                "Item No:" + "\",\"" +
                "Part No:" + "\",\"" +
                "Rev:" + "\",\"" +
                "MFG Lot No:" + "\",\"" +
                "MFG Part No:" + "\",\"" +
                "Sales Order:" + "\",\"" +
                "Description:" + "\",\"" +
                "Bulk Work Order:" + "\",\"" +
                "Quantity:" + "\",\"");

                sw.Flush();
                sw.Close();
                sw.Dispose();

        }
        else if(tubingLabel == enum2Str(MJMTubingLabelFormat::CableSquareLabel))
        {
        sw.WriteLine(
            "\"" + formOrderNumber.valueStr() + "\",\"" +
                formItemNo.valueStr() + "\",\"" +
                formPartNumber.ValueStr() + "\",\"" +
                formRev.valueStr() + "\",\"" +
                formMfgLotNum.valueStr() + "\",\"" +
                formMfgPartNum.valueStr() + "\",\"" +
                formSalesId.valueStr() + "\",\"" +
                descript + "\",\"" +
                formMFGDate.valueStr() + "\",\"" +
                formOutsideDmter.valueStr() + "\",\"" +
                formLength.valueStr() + "\",\"" +
                
                
                "Order No:" + "\",\"" +
                "Item No:" + "\",\"" +
                "Part No:" + "\",\"" +
                "Rev:" + "\",\"" +
                "MFG Lot No:" + "\",\"" +
                "MFG Part No:" + "\",\"" +
                "Sales Order:" + "\",\"" +
                "Description:" + "\",\"" +
                "MFG Date:" + "\",\"" +
                "O.D.:" + "\",\"" +
                "Quantity:" + "\",\"");

                sw.Flush();
                sw.Close();
                sw.Dispose();
        }    
        
    }

}

Str tubingLabel = formTubingLabelFormat.valueStr();

This is where the above error occurs

It sounds like formTubingLabelFormat is null - please verify it in the debugger. So it seems that MJMTubingLabel_TubingLabelFormat control doesn’t exist in the form.

By the way, you can make your life much easier if you use an extension. Then you’ll be able to use the control name directly in code and the compiler will check all names for you.

Namely, this code:

FormRun             formRun = sender.formRun();
FormComboBoxControl formTubingLabelFormat = FormRun.design(0).controlName("MJMTubingLabel_TubingLabelFormat");
Str tubingLabel = formTubingLabelFormat.valueStr();

can be replaced with this:

str tubingLabel = mjmTubingLabel_TubingLabelFormat.text();

Also, if the control is bound to a data source, you should rather use the datasource instead:

str tubingLabel = mjmTubingLabel.TubingLabelFormat;

It requires the class to be defined as an extension:

[ExtensionOf(formStr(MJMNeTubingLabel))]
final class MyClassName_Extension

And the event handler mustn’t be static:

[FormControlEventHandler(formControlStr(MJMNeTubingLabel, MJMPrintTubingLabel), FormControlEventType::Clicked)]
public void MJMPrintTubingLabel_OnClicked(FormControl sender, FormControlEventArgs e)

By the way, you should split your code to smaller methods, each with its own responsibility. It’ll make code easier to understand, and therefore easier to find bugs in it.

You should also utilize ‘using’ statement instead of disposing resources (such as StreamWriter) manually. Not only it’s easier, but more importantly it’s safer - unlike your current implementation, it’ll dispose resources even if an exception gets thrown.