Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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


What I have tried:

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); // application is failing here when putting into debug mode.
                    }
                    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);
                }
            }
        }
Posted

Whenever you see an error telling you that an Object is not set to an instance, in C#, this is telling you that something in your code is unassigned. In other words you have, somewhere in your code, a null reference. We're not going to be able to tell you where this is because we don't have all your code.

The good news is, this is very easy to find. There are a couple of ways to do this.
  1. Use the stack trace. When you encounter an error, you will get a stack trace as part of that error. Use System.Debug.Diagnostics to write out the whole exception (don't trim it to just the message).
  2. The second approach, and the one I would prefer to use, is to actually debug the code. Put a breakpoint at the start of this function, and step through it line by line. Before you execute a line, check to make sure that any properties on that line are not null. You can easily check this by hovering your mouse over it, and seeing what pops up in the tooltip.
To be honest, your first instinct should be to debug your code. It's much easier to do that than to go onto a website and post the code hoping that someone is going to be able to spot what's wrong for you. It could take you a couple of minutes to accomplish what you could be waiting for a couple of hours, to get a reply off a website.
 
Share this answer
 
Comments
Gcobani Mkontwana 20-May-24 1:57am    
this is where the issue is, 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);
}
}
That's not where the error is - that's where it shows up!

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Hi Gcobane,

Your code has an error then when it get to the catch then it throw another error due to ex.InnerException being null and when you say ex.InnerException.ToString() this give you Object not set to an instance error.

The best way to handle this is ex.InnerException?.ToString() or you just use ex.Message.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900