Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
While generating XML file for Custom class, I am getting Object set to Null reference in the following line:
 
vlist.Visibility.Add(new Visibility() { ListOrder = 2, DisplayName = "Name", Name = v.Name });
 
Can anybody tell me what am I doing wrong and how to correct it?
 
Following is my whole code :
 
#region
class Program
{
    static void Main(string[] args)
    {
        #region list GEnerateion
        Visibilities vlist = new Visibilities();
        Visibility v = new Visibility();
        v.Name = new FullName();
        vlist.AppName = "Test2";
        v.Name.FirstName = "First Name";
        v.Name.MiddleName = "Middle Name";
        v.Name.LastName = "Last Name";
        vlist.Visibility.Add(new Visibility() { ListOrder = 2, DisplayName = "Name", Name = v.Name });
        File.WriteAllText("Test1.xml", vlist.ToString());
        #endregion
    }
}
#endregion
 
#region
[Serializable]
public class FullName
{
    private String strFirstName;
    [XmlAttribute("FirstName")]
    public String FirstName
    {
        get { return strFirstName; }
        set { strFirstName = value; }
    }
 
    private String strMiddleName;
    [XmlAttribute("MiddleName")]
    public String MiddleName
    {
        get { return strMiddleName; }
        set { strMiddleName = value; }
    }
 
    private String strLastName;
    [XmlAttribute("LastName")]
    public String LastName
    {
        get { return strLastName; }
        set { strLastName = value; }
    }
 
}
#endregion
 
#region Visibility
[Serializable]
public class Visibility
{
    [XmlElement("Name")]
    public FullName Name { get; set; }
 
    [XmlAttribute("DisplayName")]
    public String DisplayName { get; set; }
 
    [XmlAttribute("ListOrder")]
    public int ListOrder { get; set; }
}
#endregion
 
#region Visibilities
[Serializable]
public class Visibilities
{
    [XmlAttribute("AppName")]
    public String AppName { get; set; }
 
    [XmlElement("Visibility")]
    public List<Visibility> Visibility { get; set; }
 
    #region
    public override string ToString()
    {
        string l_strXML = string.Empty;
 
        XmlSerializer Xml = new System.Xml.Serialization.XmlSerializer(GetType());
        MemoryStream ms = new MemoryStream();
        Xml.Serialize(ms, this);
 
        Byte[] Buffer = new byte[ms.Length];
        ms.Seek(0, 0);
        ms.Read(Buffer, 0, (int)ms.Length);
 
        l_strXML = System.Text.Encoding.ASCII.GetString(Buffer, 0, (int)ms.Length);
 
        return l_strXML;
    }
    #endregion
}
#endregion
Posted 10-May-13 20:56pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Pratik,
 
Check the below modified code. Issue resolved. FOr any queries plz let me know...
 

namespace Client.ConsoleX
{
    
    public class Program
    {
        MainServiceClient mainServiceClient = new MainServiceClient();
        public static void Main()
        {
            #region list GEnerateion
            Visibilities vlist = new Visibilities();
            Visibility v = new Visibility();
            v.Name = new FullName();
            vlist.AppName = "Test2";
            v.Name.FirstName = "First Name";
            v.Name.MiddleName = "Middle Name";
            v.Name.LastName = "Last Name";
            v.DisplayName = "Display Name";
            //vlist.Visibility.Add(new Visibility() { ListOrder = 2, DisplayName = "Name", Name = v.Name });
            //vlist.Visibility.Add(v);
            List<Visibility> list = new List<Visibility>();
            list.Add(v);
            vlist.Visibility = list;
            File.WriteAllText(@"d:\Test1.xml", vlist.ToString());
            Console.WriteLine("Sucessfully created the xml file...");
            #endregion
            
            Console.ReadKey();
        }
        
                        
    }
    #region
    [Serializable]
    public class FullName
    {
        private String strFirstName;
        [XmlAttribute("FirstName")]
        public String FirstName
        {
            get { return strFirstName; }
            set { strFirstName = value; }
        }
        private String strMiddleName;
        [XmlAttribute("MiddleName")]
        public String MiddleName
        {
            get { return strMiddleName; }
            set { strMiddleName = value; }
        }
        private String strLastName;
        [XmlAttribute("LastName")]
        public String LastName
        {
            get { return strLastName; }
            set { strLastName = value; }
        }
    }
    #endregion
    #region Visibility
    [Serializable]
    public class Visibility
    {
        [XmlElement("Name")]
        public FullName Name { get; set; }
        [XmlAttribute("DisplayName")]
        public String DisplayName { get; set; }
        [XmlAttribute("ListOrder")]
        public int ListOrder { get; set; }
    }
    #endregion
    #region Visibilities
    [Serializable]
    public class Visibilities
    {
        [XmlAttribute("AppName")]
        public String AppName { get; set; }
        [XmlElement("Visibility")]
        public List<Visibility> Visibility { get; set; }
        #region
        public override string ToString()
        {
            string l_strXML = string.Empty;
            XmlSerializer Xml = new System.Xml.Serialization.XmlSerializer(GetType());
            MemoryStream ms = new MemoryStream();
            Xml.Serialize(ms, this);
            Byte[] Buffer = new byte[ms.Length];
            ms.Seek(0, 0);
            ms.Read(Buffer, 0, (int)ms.Length);
            l_strXML = System.Text.Encoding.ASCII.GetString(Buffer, 0, (int)ms.Length);
            return l_strXML;
        }
        #endregion
    }
    #endregion
}
  Permalink  
Comments
Pratik Gaikwad at 11-May-13 2:35am
   
Thank you.
Mohammed Hameed at 11-May-13 2:35am
   
You're welcome!!!
Mohammed Hameed at 11-May-13 2:36am
   
Did u understand what was the issue?
Pratik Gaikwad at 11-May-13 2:52am
   
yes... I was attempting to add list inside a list without initiating...
Mohammed Hameed at 11-May-13 2:53am
   
Very good...
Mohammed Hameed at 11-May-13 2:55am
   
Hi Pratik,
I suggest u to add this as a new question to start a new thread. As the previous solution has already been finished.
 
Thanks & I hope u understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100