简体   繁体   中英

My excel output not coming as i wanted

Helle guy,

I am trying to create a custom made excel output. But my thing is its not giving me output. Once the code reaches if (fline.Designator == null) its showing me an error.. Please have a look to my issue and give me a solution.

Codes:-

 private void button1_Click(object sender, EventArgs e)
    {
        string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);

        string line = string.Empty;
        string CarouselName = enter.Text;
        int iCarousel = 0;
        char seperator = '\t';

        SortedDictionary<string, ExcelData> lstExcel = new SortedDictionary<string, ExcelData>();
        ExcelData fline = null;
        for (int i = 0; i < strLines.Length; i++)
        {
            line = RemoveWhiteSpace(strLines[i]).Trim();
            if (line.Length == 0)
                continue;
            string[] cells = line.Replace("\"", "").Split(seperator);


            if (i > 0)
            {
                if (!lstExcel.ContainsKey(cells[1].Replace(" ", "_")))
                {
                    fline = new ExcelData();
                    lstExcel.Add(cells[1].Replace(" ", "_"), fline);
                    fline.Footprint = cells[2].Replace(" ", "_");
                    fline.Comment = cells[1].Replace(" ", "_");

                    iCarousel++;
                    if (iCarousel > 45)
                        iCarousel = 1;
                    fline.Location = CarouselName;
                }
                else
                {
                    fline = lstExcel[cells[1].Replace(" ", "_")];
                }
                fline.SrNo++;
                fline.Total++;
            }

            if (fline.Designator == null)// here its showing the error..
                // i don't know why its the code is incorrect. please help me out!!
                fline.Designator = new List<string>();
            fline.Designator.Add(cells[0].Replace(" ", "_"));

        }
        ExportInExcel(lstExcel, @"C:\Users\Stacy\Desktop\myExcel.xls");
        System.Windows.Forms.Application.Exit();

    }

Looking at your for loop, fline is never initialized.

Initially, there is a line:

ExcelData fline = null;

During your for loop, beginning at int i=0 you have an if (i > 0) that assigns values to fline within that condition. However, for the case when i=0 , fline is never assigned a value.

Therefore it follows that when you check if fline.Designator == null when i=0 in your for loop, it throws an exception. The exception is telling you that fline has not been assigned an instance of ExcelData . In other words, fline is null itself: it doesn't have any property values.

I'm not sure exactly what your if (i > 0) condition is checking, but your solution is ultimately either:

  • Changing if (i > 0) to if (i >= 0) , thereby handling the i=0 case in your regular conditions, or...
  • Instantiating fline at some point for the i=0 case. You could do this when you initially declare the variable: ExcelData fline = new ExcelData(); instead of ExcelData fline = null; (before your loop).

My guess is your error is a null reference exception. In your method you initialize fline with null :

ExcelData fline = null;

Then you enter a for loop and you start iterating from 0 ( int i = 0 ). You also have a conditio: if (i > 0) where you initialize the fline variable with an instance of ExcelData class:

fline = new ExcelData();

Note however that this happens only for i greater than 0.

So what happens when i is 0 - and that's the case during first iteration of the loop. When you first iterate i is 0 . So you skip the fline initialization inside your if statement and you continue directly to

if (fline.Designator == null)

Here you try access fline but it's never been initialized. So you get null reference exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM