object graph cannot be null exception

Question

Monday, August 5, 2013 8:03 PM

I am trying to  remove an object from a binary file using the code below, but it throws an object graph null exception????

can I solve this!!??

 

public static void Remove(string item)
        {

            FileStream fs = new FileStream("contacts.txt", FileMode.Open, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();

            int counter = 0;
            Contact temp = null;
            Contact[] tempData = new Contact[fs.Length];
            try
            {
                while ((fs.Position < fs.Length))
                {
                    temp = (Contact)bf.Deserialize(fs);
                    if (temp.Name != item)
                        tempData[counter] = temp;

                    counter++;
                  
                }

             
                fs.Close();


                fs = new FileStream("contacts.txt", FileMode.Truncate, FileAccess.Write);
                fs.Close();

               FileStream fs1 = new FileStream("contacts.txt", FileMode.Open, FileAccess.Write);
                for (int i = 0; i < tempData.Length; i++)
                {
                    bf.Serialize(fs1, tempData[i]);
                  
                }

                fs.Close();

                menu();
            }


            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message+"\n");

                menu();               
            }

  Exception:

+  $exception {"Object Graph cannot be null.\r\nParameter name: graph"} System.Exception {System.ArgumentNullException}

All replies (4)

Monday, August 5, 2013 8:06 PM

We need full exception Details, wich can bbe aquired by e.ToString().

Likely you asumed something about the binary file structure that just isn't true. Perhaps the file is even corrputed - where did you get this file?

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Monday, August 5, 2013 8:11 PM

it says Object Graph cannot be null, parameter name:graph.

i have created the file in another part of the code, i have printed the content of the file and everything is ok, but aftercalling the method above the file becomes empty, and i get the exception.


Monday, August 5, 2013 8:24 PM

it says Object Graph cannot be null, parameter name:graph.

That is the message part of the Exception. We need the other 95% of Inforamtion it carries, wich can only be extracted via e.ToString().

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Monday, August 5, 2013 8:30 PM

  x "System.ArgumentNullException: Object Graph cannot be null.\r\nParameter name: graph\r\n   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)\r\n   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)\r\n   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)\r\n   at Homework2.Program.Remove(String item) in i:\Homework2\Homework2\Program.cs:line 188" string