How to solve this problem object reference not set to an instance in printing application?

gciraman2024

Member
Joined
May 19, 2024
Messages
8
Programming Experience
3-5
Hi Team

We experience an issue with our barcode printing application, the problem is when we reprint it gives an error message below on this screen shot. When we debug it and notice on try statement is looking for a path and give this error, all the environments we set including the paths. Printing is also a problem and we struggling to fix this need help.
printing method:
private void btnReprint_Click(object sender, EventArgs e)
        {
            string paths = "", appPath = "";
            try
            {
                string reportName = "";

                string optionSelected = "";
                if (cboReprintPrintOption.SelectedValue.ToString() == "1") //Thermal Printer Small
                {
                    optionSelected = "ThermalSmall";
                    reportName = Properties.Settings.Default.ReportNameSmallThermal;

                }
                else if (cboReprintPrintOption.SelectedValue.ToString() == "2") //Thermal Printer Large
                {
                    optionSelected = "ThermalLarge";
                    reportName = Properties.Settings.Default.ReportNameLargeThermal;

                }
                else if (cboReprintPrintOption.SelectedValue.ToString() == "3") //A4
                {
                    optionSelected = "A4";
                    reportName = Properties.Settings.Default.ReportNameA4;
                }

                CreateBarcodeDataTable();

                dtReprintBarcode.Clear();

                if (optionSelected == "A4")
                {
                    if (cboReprintPrinter.Text.Contains("ZDesigner"))
                    {
                        MessageBox.Show("A4 cannot be printed to selected Printer. Please choose a different printer", "Invalid Printer");
                        return;
                    }

                    for (int i = 0; i < dgvReprintBarcode.Rows.Count - 1; i++)
                    {
                        string LabelID = dgvReprintBarcode.Rows[I].Cells["LabelID"].Value.ToString();
                        string ProdCode = dgvReprintBarcode.Rows[I].Cells["ProdCode"].Value.ToString();
                        string BatchNumber = dgvReprintBarcode.Rows[I].Cells["BatchNum"].Value.ToString();
                        string Weight = dgvReprintBarcode.Rows[I].Cells["Weight"].Value.ToString();
                        string FGprodcode = dgvReprintBarcode.Rows[I].Cells["FGprodcode"].Value.ToString();
                        string DatePrinted = dgvReprintBarcode.Rows[I].Cells["PrintDateTime"].Value.ToString();

                        dtReprintBarcode.Rows.Add("*" + LabelID.ToString().PadLeft(8, '0') + "*", ProdCode, BatchNumber, Weight, FGprodcode, DatePrinted);
                    }

                    Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
                    String Root = Directory.GetCurrentDirectory();
                    paths = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Report\" + reportName);

                    if (DevEnvironment == '1')
                    {
                        appPath = Path.Combine(path, @"Report\" + reportName);
                    }
                    else
                    {
                        appPath = Path.Combine(Application.StartupPath, @"Report\" + reportName);
                    }

                    try
                    {
                        cry.Load(appPath);
                    }
                    catch
                    {
                        if (DevEnvironment == '1')
                        {
                            appPath = Path.Combine(path, @"Report\" + reportName);
                        }
                        else
                        {
                            appPath = Path.Combine(Application.StartupPath, @"Report\" + reportName);
                        }
                    }
                    cry.SetDataSource(dtReprintBarcode);
                    crystalReportViewer1.ReportSource = cry;

                    string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
                    cry.PrintOptions.PrinterName = PrinterName;
                    printDocument1.PrinterSettings.PrinterName = cboReprintPrinter.Text.ToString();
                    cry.Refresh();
                    cry.PrintOptions.PrinterName = cboReprintPrinter.Text.ToString();
                    int numberOfPages = 0;
                    numberOfPages = Convert.ToInt32(Math.Ceiling((Convert.ToDouble(txtLabelID.Text) - Convert.ToDouble(txtLabelIdEnd.Text)) / 6));
                    cry.PrintToPrinter(1, false, 1, numberOfPages);

                }
                else if (optionSelected == "ThermalSmall" || optionSelected == "ThermalLarge")
                {
                    ReprintToThermal(reportName);
                }
                txtLabelID.Text = "";
                txtLabelIdEnd.Text = "";

                dgvReprintBarcode.DataSource = null;
                crystalReportViewer1.ReportSource = null;
                crystalReportViewer1.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString() + " Path: " + Environment.CurrentDirectory + @"\");
                if (ex.InnerException.ToString().Contains("cannot find the file"))
                {
                    MessageBox.Show("Path:  " + appPath + " " + paths);
                }
            }
        }
 

Attachments

  • Reprinting.png
    Reprinting.png
    144.3 KB · Views: 5
Last edited by a moderator:
You should have clicked on the Details button on that dialogue. I would think that you would want the details yourself anyway, but you should definitely be providing them to us. That will show you the stack trace and, hopefully, the specific line on which the exception was thrown. If it doesn't, make sure that you deploy the PDB file(s) with your app and then it should do.
 
You should have clicked on the Details button on that dialogue. I would think that you would want the details yourself anyway, but you should definitely be providing them to us. That will show you the stack trace and, hopefully, the specific line on which the exception was thrown. If it doesn't, make sure that you deploy the PDB file(s) with your app and then it should do.

Good point here is the stack trace for the error below;

System.NullReferenceException: Object reference not set to an instance of an object.
at Batcher_Barcode_Printing.BarcodePrint.btnReprint_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9241.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Batcher_Barcode_Printing
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/GMkontwana/AppData/Local/Apps/2.0/Z3ECJ6KP.06H/ON1AZ9D3.86Z/batc..tion_0000000000000000_0001.0000_229d32da2541b97f/Batcher_Barcode_Printing.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9181.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9236.0 built by: NET481REL1LAST_B
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9241.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9214.0 built by: NET481REL1LAST_B
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
CrystalDecisions.CrystalReports.Engine
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.CrystalReports.Engine/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.CrystalReports.Engine.dll
----------------------------------------
CrystalDecisions.Shared
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.Shared/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.Shared.dll
----------------------------------------
CrystalDecisions.ReportAppServer.CommLayer
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.CommLayer/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.CommLayer.dll
----------------------------------------
CrystalDecisions.ReportAppServer.ClientDoc
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.ClientDoc/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.ClientDoc.dll
----------------------------------------
CrystalDecisions.ReportAppServer.DataDefModel
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.DataDefModel/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.DataDefModel.dll
----------------------------------------
CrystalDecisions.ReportAppServer.CubeDefModel
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.CubeDefModel/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.CubeDefModel.dll
----------------------------------------
CrystalDecisions.ReportAppServer.ReportDefModel
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.ReportDefModel/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.ReportDefModel.dll
----------------------------------------
CrystalDecisions.ReportAppServer.Controllers
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.Controllers/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.Controllers.dll
----------------------------------------
CrystalDecisions.ReportAppServer.DataSetConversion
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.DataSetConversion/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.DataSetConversion.dll
----------------------------------------
System.Web
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9232.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
log4net
Assembly Version: 1.2.10.0
Win32 Version: 1.2.10.0
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/log4net/1.2.10.0__692fbea5521e1304/log4net.dll
----------------------------------------
CrystalDecisions.Shared.resources
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.Shared.resources/v4.0_13.0.4000.0_en_692fbea5521e1304/CrystalDecisions.Shared.resources.dll
----------------------------------------
CrystalDecisions.Windows.Forms
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.Windows.Forms/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.Windows.Forms.dll
----------------------------------------
CrystalDecisions.ReportSource
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportSource/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportSource.dll
----------------------------------------
CrystalDecisions.Windows.Forms.resources
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.Windows.Forms.resources/v4.0_13.0.4000.0_en_692fbea5521e1304/CrystalDecisions.Windows.Forms.resources.dll
----------------------------------------
System.Printing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Printing/v4.0_4.0.0.0__31bf3856ad364e35/System.Printing.dll
----------------------------------------
WindowsBase
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9241.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
ReachFramework
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/ReachFramework/v4.0_4.0.0.0__31bf3856ad364e35/ReachFramework.dll
----------------------------------------
System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Runtime.Caching
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Caching/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Runtime.Caching.dll
----------------------------------------
crdb_adoplus
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.4027.3480
CodeBase: file:///C:/Program%20Files%20(x86)/SAP%20BusinessObjects/Crystal%20Reports%20for%20.NET%20Framework%204.0/Common/SAP%20BusinessObjects%20Enterprise%20XI%204.0/win32_x86/crdb_adoplus.dll
----------------------------------------
CustomMarshalers
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
----------------------------------------
System.Web.Services
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Web.Services/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.Services.dll
----------------------------------------
CrystalDecisions.Web
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.Web/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.Web.dll
----------------------------------------
CrystalDecisions.ReportAppServer.XmlSerialize
Assembly Version: 13.0.4000.0
Win32 Version: 13.0.27.3480
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/CrystalDecisions.ReportAppServer.XmlSerialize/v4.0_13.0.4000.0__692fbea5521e1304/CrystalDecisions.ReportAppServer.XmlSerialize.dll
----------------------------------------
AxShockwaveFlashObjects
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/AxShockwaveFlashObjects/v4.0_1.0.0.0__692fbea5521e1304/AxShockwaveFlashObjects.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
 
Looks like you built without debug symbols, published without the .PDB files, or build for Release without the .PDB files. Your btn_ReprintClick() is over 100 lines long. There is not enough context information in the code that you presented in your original post for us to even guess which object is null.

Try running your code under the debugger and reproduce the problem so that the debugger will take you right to the offending line, or enable JIT debugging, or deploy your .PDB files with your binaries and reproduce the problem so that at least the exception information will provide the line number.
 
As a quick aside, if you have more than 20 or 25 lines of code in a function or method, that is usually a code smell that you are breaking the single responsibility principle. Consider breaking up that huge method into multiple methods. That way the next time you get an exception without line numbers, you'll have a more granular method to find the crash in. Furthermore, if you use good names for your methods, the method name maybe enough to clue you in on what is failing or why.

 
Also, it looks like you might be suffering from a double exception. Somewhere between lines 5-102, an exception was thrown, but that exception does not have an InnerException. On your line 106, your try to dereference that null InnerException, and so you now get that null dereference exception.
 
As a quick aside, if you have more than 20 or 25 lines of code in a function or method, that is usually a code smell that you are breaking the single responsibility principle. Consider breaking up that huge method into multiple methods. That way the next time you get an exception without line numbers, you'll have a more granular method to find the crash in. Furthermore, if you use good names for your methods, the method name maybe enough to clue you in on what is failing or why.


Also, it looks like you might be suffering from a double exception. Somewhere between lines 5-102, an exception was thrown, but that exception does not have an InnerException. On your line 106, your try to dereference that null InnerException, and so you now get that null dereference exception.

@Skydiver, could this be the reason why its failing to reprint?
 
@Skydiver , this what i picked up when debugging the app, this gets thrown first which could lead to the second error;

within catch statement System.ArgumentException: 'Column named Recipe_Description cannot be found.
Parameter name: columnName'



catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString() + " Path: " + Environment.CurrentDirectory + @"\"); // issue is here
if (ex.InnerException.ToString().Contains("cannot find the file"))
{
MessageBox.Show("Path: " + appPath + " " + paths);
}
}
 
That would be an issue, yes. I just looked for "Recipe_Description" on this page and post #8 is the only place it appears, so it's not mentioned directly in your original code, so I don't know where it's coming from. Debugging should tell you that though.

As for this other issue, if you're just going to catch every exception using the Exception type then you definitely should not be assuming that it has any InnerException. You can obviously see why. My guess is that you're actually looking for a FileNotFoundException as the InnerException, based on the text you're looking for. I'm also guessing that the actual exception caught will always be the same type when that is the case. In that case, that's what you should actually be catching. Start with a catch block like this:
C#:
catch (Exception ex) when (ex.InnerException is FileNotFoundException)
and then you know that it will only be executed when there is an InnerException of the correct type. You should also then find out what the type of the outer exception is and change the code to catch that type specifically with that filter.
 
Back
Top Bottom