locked
Object reference not set to an instance of an object RRS feed

  • Question

  • User360451555 posted

    am just wondering. How one earth can this be an "Object reference not set to an instance of an object"

    The line that says "foreach (TableRow itemsTableRow in itemsTable.Rows)" is throwing that damn error. I think the TableRow is instantiated on every round of the foreach loop

                try
                {
                    if (null == Page.Session["ILSPrintJobObject"])
                        throw new ApplicationException(TextHelper.Get("IlsControls_PrintItemPrint_PrintButtonClick_InvalidPrintJobException"));
    
                    var printJob = (PrintJob)Page.Session["ILSPrintJobObject"];
    
                    // Printer or PrinterElux
                    var printer = new Printer((int)printJob.PartId);
    
                    //string fileName = ILSBase.Instruction.GetAttachedInstruction(printJob);
                    var fileName = InstructionImageLocation(printJob);
                    var hasInstructionFile = !string.IsNullOrEmpty(fileName);
    
                    mainPage = Parent.Parent;
    
                    itemsTable = (Table)mainPage.FindControl("printSelectPlaceHolder")
                                               .Controls[0].FindControl("itemPlaceHolder")
                                               .FindControl("itemsTable");
    
                    var itemIdsAndQuantitiesList = new List<ItemsQuantities>();
    
                    foreach (TableRow itemsTableRow in itemsTable.Rows)
                    {
                        if (itemsTable.Rows.GetRowIndex(itemsTableRow) == 0) continue;
    
                        string  currentItemQty = null, currentItemId = null;
    
                        foreach (TableCell cell in itemsTableRow.Cells)
                        {
                            if (itemsTableRow.Cells.GetCellIndex(cell) == 0)
                                currentItemQty = ((TextBox)cell.Controls[0]).Text;
                            else if (itemsTableRow.Cells.GetCellIndex(cell) == 1)
                                currentItemId = cell.Text;
                        }
                        itemIdsAndQuantitiesList.Add(new ItemsQuantities
                        {
                            ItemID = currentItemId,
                            Qty = currentItemQty
                        });
                    }
    
                    printJob.ItemsAllQty = itemIdsAndQuantitiesList.Where(x => x.Qty.Trim().Equals("0")).ToList();
    
                    Page.Session["ILSPrintJobObject"] = printJob;
    
                    string sFile = string.Join(",", printer.GeneratePrintFile(printJob).ToArray());
                    string jsCommand = printer.GeneratePrintCommand(userName, sFile, printJob.Label, printJob.Workstation);
                    Page.ClientScript.RegisterStartupScript(GetType(), "PrintScript", jsCommand, false);
    
                    // Aplication instruction? 
                    if (hasInstructionFile)
                    {
                        string url = "/" + ILSConfig.AppName + "/request/Instruction.aspx";
                        //string script = "DisplayApplicationInstruction('" + url + "', 500, 200);";
                        string script = "$('#overlay-image-location').attr('src', '" + fileName + "');";
    
                        if (hasInstructionFile) script += "\nshowPopup();\n";
    
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "ApplicationScript", script, true);
                    }
                }
                catch (Exception exp)
                {
                    Logger.RecordMessage(exp.Message, ILSBase.Log.LogLevels.Error);
                    OnError(exp);
                }

    Tuesday, August 14, 2018 9:18 PM

All replies

  • User409696431 posted

    Debug the code and see what the value for itemsTable is.

    Tuesday, August 14, 2018 9:53 PM
  • User465171450 posted

    Did you make sure that itemsTable actually exists after the FindControl? If not it will just blow up immediately upon hitting the foreach.

    Tuesday, August 14, 2018 10:04 PM