1

I am doing a 4 steps of process with a 100+ of XML files and write them into a specific location. SO Initially i used a parallel.for to get each of the file and that Parallel loop calling a method which is in another cs file..my process consists of 4 steps..where the first 3 is fetching and modifying the Elements with some conditions and the 4 rth step is writing that into a location..so for that(4th step alone) i have used a lock, So that at a time a single thread can have rights for text writer..so.1st 9 to 11 files processed without the error with locks...after that i am getting a object ref not set to an instance of the object.. i have used concurrent dictionary and a Class object to hold the Data..THE Class object comes as null after the 9 th file... kindly give me an idea to resolve this.

//Parallel Loop in Form1.cs

XMLDEFN WebXML = new XMLDEFN();
DirectoryInfo dinfo = new DirectoryInfo(@"E:\XMLLIST");
FileInfo[] xmllist = dinfo.GetFiles("*.xml", SearchOption.AllDirectories);
int count1 = xmllist.Count();
object lockobj = new object();
Parallel.For(0, count1, i =>
{
    WebXML.FileName = xmllist[i].ToString();
    WebXML.Migrate(lockobj, i);    //this call goes to the another Project CS file

});

Project2.migartor.cs

public override bool Migrate(object lockobj,int i)
{
    ProcessContext();
    ProcessTasks();
    ProcessServices();
    ProcessDataTransfer();
    IncludeVersionInfo();
    lock (lockobj)
    {
        Save(lockobj,i);
    }
    return true;
}

protected void Save(object lockobj,int i)
{
    int a = i;
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(OBJ_M));
    XmlSerializerNamespaces ns = null;
    TextWriter txtWriter = null;
    ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    // Create an instance of stream writer.
    txtWriter = new StreamWriter(@"d:\temp\" + OBJ.ILBOName + "_temp.xml");
    xmlSerializer.Serialize(txtWriter, OBJ, ns);

    // Close the stream writer
    txtWriter.Close();
    txtWriter = null;
    xmlSerializer = null;
    OBJ = null;
}

THE OBJ is coming as null... oBJ comes with some 10+attributes which is updated in the Above 3 steps.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Raji
  • 43
  • 6
  • Side note: There is something wrong with your keyboard... Please check if shift if just stuck... – Alexei Levenkov Nov 28 '14 at 07:33
  • 1
    There is no way to suggest anything without reasonable sample showing the problem. Please edit your post and add sample demonstrating the issue. – Alexei Levenkov Nov 28 '14 at 07:34
  • Your Parallel.For() looks like it can't really work... How stateful is the `FileName` property? – H H Nov 29 '14 at 21:27
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 29 '14 at 21:29
  • OBJ is undefined. Relevant code is not present. Unanswerable as it stands. Closing. – usr Nov 30 '14 at 09:14
  • public class OBJ_CLASS { public OBJ_CLASS(); [XmlAttribute] public string ILBOName { get; set; } [XmlAttribute] public ILBOTYPE ILBOType { get; set; } [XmlAttribute] public string InitService { get; set; } [XmlAttribute] } – Raji Dec 01 '14 at 04:38

1 Answers1

0

you are creating a new XmlSerializerNamespaces object and adding a null namespave to that object.

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

according to msdn (linked here: http://msdn.microsoft.com/en-us/library/ms163161(v=vs.110).aspx) you make a test which run on your object properties and matches each and evry property with a null namespace. the proper writing of such a scenario would be like:

public class MyClass 
{
    [XmlElement(Namespace = "http://www.example1.com")]
    public string sample1;

    [XmlElement(Namespace = "http://www.example2.com")]
    public string sample2;

    //....
}

//...

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("prefix", "http://www.example2.com");

this is clearly not your case... please call your serialize method like this:

xmlSerializer.Serialize(txtWriter, OBJ);
ymz
  • 6,602
  • 1
  • 20
  • 39
  • Changing the Serialize method,not giving a any +ve Result..still getting the Same Error..And the Code is working fine with the Normal foreach.adding the Parallel.foreach and Lock,throws me an Error..Any suggestions? – Raji Dec 01 '14 at 04:34
  • your are using a single object 'WebXM' in a parallel loop... when you change WebXML.FileName, the property switches values before calling WebXML.Migrate.. try to use a single nethod (Migrate) which will get the FileName as a parameter – ymz Dec 01 '14 at 17:45
  • Not Working..Any other suggestions? – Raji Dec 02 '14 at 10:12