0

I keep getting a NullReferenceException at this line UserRoot.Element("User_ID").Value = User.User_ID.ToString();

What exactly am I doing wrong?

Here's the majority of the code for that method

            if (File.Exists(Path2UserDB + User.User_ID.ToString() + ".db") == false)
            {
                File.Create(Path2UserDB + User.User_ID.ToString() + ".db");

            }
            XElement UserRoot = new XElement("User");
            UserRoot.Element("User_ID").Value = User.User_ID.ToString();
            UserRoot.Element("Full_Name").Value = User.Full_Name;
            UserRoot.Element("Gender").Value = User.Gender;
            UserRoot.Element("BirthDate").Value = User.BirthDate.ToString();
            UserRoot.Element("PersonType").Value = User.PersonType.ToString();
            UserRoot.Element("Username").Value = User.Username;
            UserRoot.Element("Password").Value = User.Password;
            UserRoot.Element("Email_adddress").Value = User.Email_Address;
            XDocument UserDoc = new XDocument();
            UserDoc.Save(Path2UserDB + User.User_ID.ToString() + ".db");

Thanks

I know that saving Usernames and Passwords in plain text is incredibly unsafe, but this is only going to be accessed by one process that I will eventually implement strong security

0

3 Answers 3

2

The Element("User_ID") method returns an existing element named <User_ID>, if any.

Since your XML element is empty, it returns null.

You should create your XML like this:

var userDoc = new XDocument(
    new XElement("User",
        new XElement("User_ID", User.User_ID),
        new XElement("Full_Name", User.Full_Name),
        new XElement("Gender", User.Gender),
        ...
    )
);

Alternatively, you can call the Add method to add a node to an existing element.

Sign up to request clarification or add additional context in comments.

Comments

2

You are getting this error, because there is no XML element called User_ID under UserRoot to set its value. If you comment it out, you will get the same error on the next line and so on for every other Element, since you haven't added Elements with thos names. To create the tree that you want, try this:

XElement UserRoot =
    new XElement("User",
        new XElement("User_ID", User.User_ID.ToString()),
        new XElement("Full_Name", User.Full_Name), 
        new XElement("Gender", User.Gender),
        new XElement("BirthDate", User.BirthDate.ToString()),
        new XElement("PersonType", User.PersonType.ToString()),
        new XElement("Username", User.Username),
        new XElement("Password", User.Password),
        new XElement("Email_adddress", User.Email_Address)
    );

The following MSDN link on XML Tree Creation with XElement will be of help.

2 Comments

Both you and the above guy are right, I would checkbox both if I could
@Indebi no problem; by the time I posted my solution, I noticed that Slaks had already updated his answer to include the proper code, so he should get the checkmark. I am happy that it all worked out for you though :)
0

You want to check if the value is null or empty before running methods on it.

if(!String.IsnullorEmpty(User.User_ID))
       UserRoot.Element("User_ID").Value = User.User_ID.ToString();

2 Comments

I think the problem is that UserRoot.Element("User_ID") is null, not User.User_ID
Gotcha, well he should check that too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.