NullReferenceException when trying to add an XElement to an existing XElement

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im writing a grade tracker application for Windows Phone 7, and am storing all semester, class, and grade information in an XML file. I begin the process by creating the XML file with an XMLWriter

<div style="color:black; background-color:white
<pre><span style="color:blue Using isoStore <span style="color:blue As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
<span style="color:blue Using isoStream <span style="color:blue As <span style="color:blue New IsolatedStorageFileStream(<span style="color:#a31515 "Graders.xml", FileMode.Create, isoStore)
<span style="color:blue Dim mysettings <span style="color:blue As <span style="color:blue New XmlWriterSettings
mysettings.ConformanceLevel = ConformanceLevel.<span style="color:blue Auto
mysettings.Indent = <span style="color:blue True
<span style="color:blue Dim mywriter <span style="color:blue As XmlWriter = XmlWriter.Create(isoStream, mysettings)
mywriter.WriteStartDocument()
<span style="color:green Write Root Element
mywriter.WriteStartElement(<span style="color:#a31515 "ClassTracker")
<span style="color:green Write Background Color ARGB values as Attributes
mywriter.WriteAttributeString(<span style="color:#a31515 "BA", <span style="color:#a31515 "0")
mywriter.WriteAttributeString(<span style="color:#a31515 "BR", <span style="color:#a31515 "0")
mywriter.WriteAttributeString(<span style="color:#a31515 "BG", <span style="color:#a31515 "0")
mywriter.WriteAttributeString(<span style="color:#a31515 "BB", <span style="color:#a31515 "0")
mywriter.WriteAttributeString(<span style="color:#a31515 "BUA", <span style="color:#a31515 "255")
mywriter.WriteAttributeString(<span style="color:#a31515 "BUR", <span style="color:#a31515 "46")
mywriter.WriteAttributeString(<span style="color:#a31515 "BUG", <span style="color:#a31515 "46")
mywriter.WriteAttributeString(<span style="color:#a31515 "BUB", <span style="color:#a31515 "46")
<span style="color:green Write total attribute
mywriter.WriteAttributeString(<span style="color:#a31515 "total", <span style="color:#a31515 "0")
<span style="color:green Write Child Element (Semesters)
mywriter.WriteStartElement(<span style="color:#a31515 "Semesters", <span style="color:#a31515 "Semesters")
<span style="color:green Write child attributes
mywriter.WriteAttributeString(<span style="color:#a31515 "total", mytotal)
mywriter.WriteAttributeString(<span style="color:#a31515 "selected", <span style="color:#a31515 "0")
mywriter.WriteEndElement()
mywriter.WriteEndDocument()
mywriter.Flush()

isoStream.Close()

<span style="color:blue End <span style="color:blue Using
<span style="color:blue End <span style="color:blue Using
[/code]


When I click the "Add Semester" button, it brings up an InputPrompt asking for the name. The name is then passed along as the string SemesterName to MakeNewClass:


<div style="color:black; background-color:white
<pre><span style="color:blue Private <span style="color:blue Sub MakeNewClass()
<span style="color:blue Dim isoStore <span style="color:blue As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
<span style="color:blue Dim isoStream <span style="color:blue As IsolatedStorageFileStream

isoStream = isoStore.OpenFile(<span style="color:#a31515 "Graders.xml", FileMode.Open)

<span style="color:green Loads XML File from Isolated Storage
MyFile = XDocument.Load(isoStream)
isoStream.Close()
isoStream = <span style="color:blue New IsolatedStorageFileStream(<span style="color:#a31515 "Graders.xml", FileMode.Create, isoStore)
<span style="color:blue Dim myelement <span style="color:blue As <span style="color:blue New XElement(<span style="color:#a31515 "Semester" + mytotal.ToString)
<span style="color:blue Dim myattribute <span style="color:blue As <span style="color:blue New XAttribute(<span style="color:#a31515 "Name", SemesterName)
<span style="color:green This is where I get the NullReferenceException (mytotal is an integer=0
MyFile.Root.Element(<span style="color:#a31515 "Semesters").Add(<span style="color:blue New XElement(<span style="color:#a31515 "Semester" + mytotal.ToString))
MyFile.Root.Element(<span style="color:#a31515 "Semesters").Element(<span style="color:#a31515 "Semester" + mytotal.ToString).Add(<span style="color:blue New XAttribute(<span style="color:#a31515 "Name", SemesterName))
MyFile.Root.Element(<span style="color:#a31515 "Semesters").Element(<span style="color:#a31515 "Semester" + mytotal.ToString).Add(<span style="color:blue New XAttribute(<span style="color:#a31515 "Classes", <span style="color:#a31515 "0"))
MyFile.Root.Element(<span style="color:#a31515 "Semesters").Element(<span style="color:#a31515 "Semester" + mytotal.ToString).Add(<span style="color:blue New XAttribute(<span style="color:#a31515 "selected", <span style="color:#a31515 "0"))
MyFile.Root.SetAttributeValue(<span style="color:#a31515 "total", (mytotal + 1).ToString)
mytotal += 1

MyFile.Save(isoStream)

isoStream.Close()

<span style="color:blue End <span style="color:blue Sub
[/code]


At the comment above, the program throws a NullReferenceException:

System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
at Grade_Tracker.StartPage.MakeNewClass()
at Grade_Tracker.StartPage.input_Completed(Object sender, PopUpEventArgs`2 e)
at Coding4Fun.Phone.Controls.PopUp`2.OnCompleted(PopUpEventArgs`2 result)
at Coding4Fun.Phone.Controls.UserPrompt.ok_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
at Microsoft.Xna.Framework.Input.UnsafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
at Microsoft.Xna.Framework.Input.SafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
at Microsoft.Xna.Framework.Input.WindowMessageHooker.Hook.WndProc(IntPtr msgWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
InnerException: Unable to evaluate expression.


I have checked, and MyFile looks like this at the time of the exception:

<div style="color:black; background-color:white
<pre><span style="color:blue <<span style="color:#a31515 ClassTracker <span style="color:red BA<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red BR<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red BG<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red BB<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red BUA<span style="color:blue =<span style="color:black "<span style="color:blue 255<span style="color:black " <span style="color:red BUR<span style="color:blue =<span style="color:black "<span style="color:blue 46<span style="color:black " <span style="color:red BUG<span style="color:blue =<span style="color:black "<span style="color:blue 46<span style="color:black " <span style="color:red BUB<span style="color:blue =<span style="color:black "<span style="color:blue 46<span style="color:black " <span style="color:red total<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black "<span style="color:blue > <span style="color:blue <<span style="color:#a31515 Semesters <span style="color:red total<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red selected<span style="color:blue =<span style="color:black "<span style="color:blue 0<span style="color:black " <span style="color:red xmlns<span style="color:blue =<span style="color:black "<span style="color:blue Semesters<span style="color:black " <span style="color:blue /> <span style="color:blue </<span style="color:#a31515 ClassTracker<span style="color:blue >
[/code]



To add insult to injury, when I do not write the Element "Semesters" with the XMLWriter, but add it later using

<div style="color:black; background-color:white
<pre>MyFile.Root.Add(<span style="color:blue New XElement(<span style="color:#a31515 "Semesters"))
[/code]

Everything works perfectly.

I cannot figure out what is going on. Should I just use the XDocument.Add for all of my Elements and Attributes? Have I used the XMLWriter wrong? I could use my work-around, but I want to know what I did wrong and understand my mistake instead of just jury-rigging
it. Any help would be appreciated.












<br/>
<br/>

View the full article
 
Back
Top