0

I'm trying to make a simple console program to read and write to a text file, which I found on the internet. But there are several object-reference errors that hinder me and i can't seem to find a solution or satisfying answer to my problem. There is also an error partaining the text file already being in us by another process. They are located between //--------------- in the code. Please advice

namespace SavingNames
{
class Program
{
    public static void Main(string[] args)
    {
        Naam[] personen = new Naam[1];
        int C;

        do
        {
            //menu
            Console.WriteLine("1. Load data.");
            Console.WriteLine("2. Display persons.");
            Console.WriteLine("3. Add new Person.");
            Console.WriteLine("4. Exit.");
            Console.WriteLine("Make a choice 1-4: ");
            C = Convert.ToInt32(Console.ReadLine());

            switch (C)
            {
                case 1:
                    GetData(ref personen);

                    break;
                case 2:
                    ShowAll(personen);
                    break;
                case 3:
                    AddPerson(ref personen);
                    break;
                case 4:
                    break;
            }
        } while (C != 4);

    }


    public static void GetData(ref Naam[] personen)
    {
        StreamReader reader = new StreamReader("Names.txt");
        int size = 0;

        using (reader = File.OpenText("Names.txt"))
        {
            while (reader.ReadLine() != null)
            {
                size++;
            }
        }

        personen = new Naam[size];
        reader = File.OpenText("Names.txt");

        for (int i = 1; i < personen.Length; i++)
        {
            personen[i] = new Naam();
            personen[i].Name = reader.ReadLine();
            personen[i].Livingplace = reader.ReadLine();
            personen[i].Profession = reader.ReadLine();
        }

        reader.Close();

        Console.WriteLine("Data loaded. Press any key to continue...");
        Console.ReadKey();
        Console.Clear();
    }

    public static void ShowAll(Naam[] personen)
    {
        Console.Clear();

        for (int i = 0; i < personen.Length; i++)
        {
           //---------------------------------------------------
           personen[i].Display();//error object-reference-not-set-to-an-instance-error happens here when
                                 //I try to display all entries.
           //---------------------------------------------------
        }

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();

    }

    public static void AddPerson(ref Naam[] personen)
    {
        //----------------------------------------------------------
        StreamWriter writer = new StreamWriter("Names.txt", true);//here I get an error saying that the file is already opened
        //writer = File.AppendText("Names.txt");                  //by another process (probably the getdata function)
//--------------------------------                                               //and I don't know how to get past this

        //writer.WriteLine(personen.Length + 1);

        Naam temp = new Naam();

        //input gegevens nieuwe persoon
        Console.WriteLine("Name: ");
        temp.Name = Console.ReadLine();

        Console.WriteLine("Hometown: ");
        temp.Livingplace = Console.ReadLine();

        Console.WriteLine("Profession: ");
        temp.Profession = Console.ReadLine();

        //schrijf nieuwe gegevens naar tekst file.
        writer.WriteLine(temp.Name);
        writer.WriteLine(temp.Livingplace);
        writer.WriteLine(temp.Profession);

        //geef de vorige gegevens in.
        for (int i = 0; i < personen.Length; i++)
        {
            //-----------------------------------------
            writer.WriteLine(personen[i].Name);         //error object-reference-not-set-to-an-instance-error happens here when
                                                        //I try to add a new person even 
            writer.WriteLine(personen[i].Livingplace);
            writer.WriteLine(personen[i].Profession);
            //-----------------------------------------
        }

        writer.Close();

        //update array.
        GetData(ref personen);
    }
}

}

  • Object reference error as in NullReferenceException? What are the errors exactly? – Broots Waymb Jul 21 '17 at 16:37
  • You never put anything in personen - of course you are getting NullReferenceExceptions when you try to call methods on its contents, because there are no contents. If you want value semantics here instead then you should change Naam to a struct. – hoodaticus Jul 21 '17 at 16:38

0 Answers0