I am getting this error object reference not set to an instance of an object and cannot figure out how to fix it. I can get the program to when I only have one Timepiece object, but when I try more I get the error message. I left a note of where the error is coming from in main
class program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace P3
{
public class TimePiece
{
private static int num = 99;
private int ID;
private int hoursTime;
private string minute;
private string m;
public TimePiece()
{
}
public void Time(int hoursTime, string minute, string m)
{
this.hoursTime = hoursTime;
this.minute = minute;
this.m = m;
ID= num;
num++;
}
public void display()
{
Console.WriteLine(hoursTime + " " + minute + " " + m +" " + ID);
}
}
}
main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace P3
{
class Program
{
static void Main(string[] args)
{
int limit = 15;
TimePiece []timepiece = new TimePiece[limit];
string line=" ";
//int toggleCount;
string filename = "f:\\date.txt";
FileStream f = new FileStream(filename, FileMode.Open);
StreamReader stream = new StreamReader(f);
for (int i = 0; i < limit; i++)
{
//maybe bring in strin and do spit in constuctor.
line = stream.ReadLine(); // header line
string[] array = line.Split(' ');
int milTime = Convert.ToInt32(array[0]);
string pieceID = array[1];
string tog = array[2];
timepiece[i].Time(milTime, pieceID, tog);//this is the line that causes the error
}
stream.Close();
for (int i = 0; i < limit; i++)
{
timepiece[i].display();
}
}
}
}