Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi people,
 
Can somebody please tellme why i'm getting an Index out of range exception with the following code.
 
The idea is that i want to read a CSV file and output the contents into a ListView. Columns 1, 2 and 3 are always data, column 4 is for notes and therefore does not alwasy contain data, hense my 'if (data[3] != null)'.
 
If i make sure all columns in the CSV have data, and remove the if statement, no problems! I'm clearly making a schoolboy error somewhere. Anybody like to show me where? Smile | :)
 

public void readFromCSV()
        {
            using (FileStream fs = new FileStream("O:\\TestDaws\\CSDB\\Test2.csv", FileMode.Open, FileAccess.Read))
            {
                using (TextReader sr = new StreamReader(fs))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] data = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = data[0];
                        lvi.SubItems.Add(data[1]);
                        lvi.SubItems.Add(data[2]);
                        if (data[3] != null) //Index out of range exception thrown here!
                        {  
                            lvi.SubItems.Add(data[3]);
                                              
                        }
                        listView1.Items.Add(lvi);
                    }
                    
                }
            }
        }
Posted 25-Jan-13 1:50am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It throws an error because you are trying to check a value that doesn't exist! Instead of
                        if (data[3] != null)
Try
                        if (data.Length >= 4)
  Permalink  
Comments
DaedalusAero at 25-Jan-13 7:58am
   
Many thanks indeed. Perfect.
OriginalGriff at 25-Jan-13 8:25am
   
You're welcome!
Milind_T at 25-Jan-13 7:58am
   
Nice and fast catch that was :-)
OriginalGriff at 25-Jan-13 8:26am
   
We've all done it - and I try to remember my mistakes (if only because Herself never forgets them either... :laugh:)
SChristmas at 25-Jan-13 8:37am
   
Good catch OriginalGriff

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 403
2 Sergey Alexandrovich Kryukov 195
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 4,958
2 Maciej Los 3,269
3 Peter Leow 3,129
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100