无法访问列表中的第二项

时间:2012-12-13 19:13:22

标签: c# list class

我无法访问列表中的第二项,我看不出为什么会有任何帮助。

class Datapoints
    {
        List<Datapoint> datapoints;                                         //Creates a list of datapoints from Datapoint
        private string fileName;
        public string Filename { get { return fileName; } }

        public Datapoint GetDatapoint(int i)                                //Creates an instance of a Datapoint called GetDatapoint                        
        {
            if (i < datapoints.Count)
                return datapoints[i];
            else
                return null;
        }

        public Datapoints(string fName)                                     //Method that creates a new list of datapoints with the objects of Datapoint within it
        {
            this.fileName = fName;
            datapoints = new List<Datapoint>();
            TextReader tr = new StreamReader(fileName);
            string input;
            while ((input = tr.ReadLine()) != null)
            {
                string[] bits = input.Split(',');
                Datapoint a = new Datapoint(bits[0], bits[1], bits[2]);
                datapoints.Add(a);
            }
            tr.Close();

        }

这是表格,我想要做的是按下下一个按钮时框中显示文件中的三个数字。

private void InitTextBoxes()
        {
            if (myDatapoints.Count > 0)
            {
                Datapoint a = myDatapoints.getItem(0);
                textBoxLatitude.Text = a.Latitude;
                textBoxLongtitude.Text = a.Longtitude;
                textBoxElevation.Text = a.Elevation;
                buttonNext.Enabled = true;
                buttonPrevious.Enabled = true;
            }
            count = 0;
            textBoxLatitude.Enabled = false;
            textBoxLongtitude.Enabled = false;
            textBoxElevation.Enabled = false;
            buttonDone.Visible = false;
            buttonDone.Enabled = false;
            addingData = false;
            saved = true;
            openToolStripMenuItem.Enabled = false;
        }


        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Csv Files (*.csv)|*.csv|Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (ofd.ShowDialog(this).Equals(DialogResult.OK))
            {
                myDatapoints = new Datapoints(ofd.FileName);
                this.Text = "Data Entry - " + ofd.SafeFileName;
                InitTextBoxes();
                if (myDatapoints.Count > 0)
                {
                    buttonNext.Enabled = true;
                    buttonPrevious.Enabled = true;
                    closeToolStripMenuItem.Enabled = true;
                    saveAsToolStripMenuItem.Enabled = true;
                }
            }

        }



        private void buttonNext_Click(object sender, EventArgs e)
        {


                count++;
                if (count == myDatapoints.Count)
                    count = 0;

            Datapoint a = myDatapoints.getItem(count);
            textBoxLatitude.Text = a.Latitude;
            textBoxLongtitude.Text = a.Longtitude;
            textBoxElevation.Text = a.Elevation;

            textBoxTest.Text = Convert.ToString(myDatapoints.Count);

        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {

        } 

另外,当我启动框时,我无法将datapoints.GetItem更改为除了(0)以外的任何内容,就好像我在下面的行中有一个错误我相信这是问题的核心,但我不明白为什么。错误是

  

对象引用未设置为对象的实例。

在我用来测试的文件中有9个数字(1,2,3,4,5,6,7,8,9)所以当我选择1时它应该取中间三个,它不是“T!

 internal Datapoint getItem(int p)
        {
            if (p < datapoints.Count)
            {
                return datapoints[p];
            }
            else
                return null;
        }

1 个答案:

答案 0 :(得分:0)

在datapoints构造函数中,您只存储前三个(如果您的文件只包含一行)

将文件格式更改为每行只有3个数字 例如:
1,2,3
4,5,6
7,8,9

或更改逻辑以存储来自单行ex的所有值:

   while ((input = tr.ReadLine()) != null)
        {
            string[] bits = input.Split(',');
            if (bits.Length % 3 != 0)
            {
               //error bad file 
            }
            else
            {
               int count = bits.Length / 3;
               int i = 0;
               while (count > 0)
               {
                   Datapoint a = new Datapoint(bits[i], bits[i+1], bits[i+2]);
                   datapoints.Add(a);
                   i += 3;
                   count--;
               }
             }
        }