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();
                          
            }
             }
    }
}

What is in the file that you are opening? Can you give the data, and i should be able to provide an answer.

I don't think this particular error has to do with the file.

TimePiece [] timePiece = new TimePiece[limit]

creates an array with room for Timepiece objects, but does not instantiate the individual objects

Around line 27 (but certainly before 33) you need something to the effect of

timepiece[i] = new TimePiece();

It is just a simple text file

06 OO AM
06 15 AM
06 OO PM
06 15 PM
07 OO AM
07 15 AM
07 OO PM
07 15 PM
08 OO AM
08 15 AM
08 OO PM
08 15 PM
09 OO PM
09 15 PM
10 OO AM
11 15 AM
11 OO PM
11 15 PM

I am also having trouble parsing the second group of numbers when I use int pieceID = Convert.ToInt32(array[1]); I get a run time error input string is not in correct format

See my edit, I had the name of your class wrong...

Post the code you tried for the pieceID part in the context so I can try it out.

When I pieceID to a string it runs fine, but when I try to part is I get a run time error, input string was not in a correct format.
This is 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]);
                int pieceID = Convert.ToInt32(array[1]);
                string tog = array[2];
                timepiece[i] = new TimePiece(milTime, pieceID, tog);
              

            }
            stream.Close();
            for (int i = 0; i < limit; i++)
            {

                timepiece[i].display();
             
               
            }
          

        }
    }
}

This is the class
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 int minute;
private string m;

public TimePiece(int hoursTime, int minute, string m)
{
this.hoursTime = hoursTime;
this.minute = minute;
this.m = m;
ID = num;
num++;
}


public void display()
{
Console.WriteLine(hoursTime + " " + minute + " " + m +" " + ID);
}
//Precondition-none
//Postcondition- ID is incremented by one.
public int IDnum()
{
num++;
return num;
}

}
}

Try using Int32.TryParse instead:

string str = "42"; //your string
int numb; //the number output from the method
if(!Int32.TryParse(str,out numb))
{
  //it failed
}

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.