3

So I have a Serializable class Student, and I want my ReadFromFile method to deserialize my file so I can know how many records I already have in my object, so that when I want to add new records to my array I can know what's the index of the last array and I can put my new record in the index number after that. the function gives me an error on the 2nd pass of "Console.WriteLine(st2[j].FName + " " + st2[j].LName);" and tells me

NullReferenceException was unhandled

and it just writes the first item in my record that I have, not the rest.

public static int ReadFromFile()
{
    int j = 0;
    string path = @"students.dat";

    try
    {
        Students[] st2 = new Students[100];

        BinaryFormatter reader = new BinaryFormatter();
        FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read);

        st2 = (Students[])reader.Deserialize(input);

        while (true)
        {
            st[j] = new Students();
            Console.WriteLine(st2[j].FName + " " + st2[j].LName);
            j++;
        }

        Console.WriteLine("there are " + j + "students in the file");

        input.Close();
        return j;
    }
    catch (FileNotFoundException)
    {
        Console.WriteLine("there are no student records yet.");
        return j;
    }
}

this is my Serialization method:

public static void WriteInFileFromInput(Students[] x)
    {

        string path = @"students.dat";

        if (File.Exists(path))
        {
            BinaryFormatter Formatter = new BinaryFormatter();
            FileStream output = new FileStream(path, FileMode.Append, FileAccess.Write);

            Formatter.Serialize(output, st);

            output.Close();
        }

        else
        {
            BinaryFormatter Formatter = new BinaryFormatter();
            FileStream output = new FileStream(path, FileMode.CreateNew, FileAccess.Write);

            Formatter.Serialize(output, st);

            output.Close();
        }
    }
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
Lily
  • 99
  • 1
  • 3
  • 8
  • Is there a reason why you use array Students[] and not dynamic collection such as Collection ? – Ondrej Svejdar Dec 12 '13 at 13:20
  • I've never worked with lists or collections so this was the only way I thought of. – Lily Dec 12 '13 at 13:27
  • I would put that `FileStream` in a `using` block: `using (FileStream output = new FileStream(...)){...}`. It will cause cleanup even if there's an exception. – John Saunders Dec 12 '13 at 13:29
  • `while(true) { }` loops endlessly unless an exception is thrown (NullReferenceException, IndexOutOfRangeException). – JeffRSon Dec 12 '13 at 13:30
  • `Formatter.Serialize(output, st);` - whence comes the `st`? – Matthew Watson Dec 12 '13 at 13:35
  • This code is bonkers. The first value assigned to `s2` is just thrown away. The serializer overwrites it with a new array. ALso, what does this `st[j] = new Students();` achieve? – Gusdor Dec 12 '13 at 13:37
  • I defined `public static Students[] st = new Students[100]` at the beginning of the class, and I have a Write() function that writes the information of students in an object array. and then that object is serialized in WriteInFileFromInput. – Lily Dec 12 '13 at 13:40

1 Answers1

0

The proper loop should look like this (assuming the data was serialized correctly):

foreach (var student in st2) // Replaces the while loop in the OP
{
    Console.WriteLine(student.FName + " " + student.LName);
    ++j;
}

However, I think there is an error in the serialization so this will still give a null reference exception. If so, can you post the code that serializes the data?

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • that didn't solve it, unfortunately. I edited the post with the serialization function. – Lily Dec 12 '13 at 13:26
  • I changed a few things outside of the serialization functions, and now I can have all of the records in my array, but I still get the same error apparently because the array I have has still has places for 98 more records. is that the problem? – Lily Dec 12 '13 at 14:01