Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot access the second item in the list
    text
    copied!<p>I am having trouble accessing the second item in the list and I cannot see why any help would be appreciated.</p> <pre><code>class Datapoints { List&lt;Datapoint&gt; 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 &lt; 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&lt;Datapoint&gt;(); 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(); } </code></pre> <p>Here is the form, what I am trying to make happen is to have the boxes show the three numbers in the file when pressing the next button.</p> <pre><code>private void InitTextBoxes() { if (myDatapoints.Count &gt; 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 &gt; 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) { } </code></pre> <p>Also, when I initiate the boxes, I cannot change the datapoints.GetItem to anything but (0) as if i do there is an error in the following line I believe this is the heart of the problem but I cannot see why. The error is </p> <blockquote> <p>Object reference not set to an instance of an object. </p> </blockquote> <p>Within the file I am using to test there are 9 numbers (1,2,3,4,5,6,7,8,9) so it ought to take the middle three when I select 1, bit it doesn't!</p> <pre><code> internal Datapoint getItem(int p) { if (p &lt; datapoints.Count) { return datapoints[p]; } else return null; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload