I have a windows phone 7.1 app that tries to get data from a web service. The data is in XML format. Language is vb.net under Visual Studio 2010 with the Async CTP 3.
I have successfully retrieved the response from the web service and parsed the string into an XDocument. But I have problem when trying to populate a viewmodel by looping through the XML because the loop variable contains nothing (I want it to contain XElement):
Private Async Function GetMovies() As Task
Dim client As New WebClient
Dim address = (New Uri(New Uri(APIROOT), "movieschedule/movies")).ToString
Dim resulttxt = Await client.DownloadStringTaskAsync(address)
Dim resultdoc = XDocument.Parse(resulttxt)
Dim resultlist As New Collections.ObjectModel.ObservableCollection(Of Movies)
For Each movie In resultdoc.Descendants("movie")
' **the variable 'movie' contains nothing**
Dim m As New Movies With {
.Cast = movie.Attribute("cast").Value,
...
.Title = movie.Attribute("title").Value
}
resultlist.Add(m)
Next
Movies = resultlist
End Function
Example of XML from the web service:
<movies>
<movie code="MOV1299" title="Age of Heroes" is3D="0" genre="THRILLER" rating="D" cast="Sean Bean, Danny Dyer, Izabella Miko, James D'Arcy, Sebastian Street, William Houston" director="Adrian Vitoria" language="ENGLISH" subtitle="BAHASA INDONESIA" />
<movie code="MOV1325" title="Fast and Furious 5" is3D="0" genre="ACTION" rating="D" cast="Vin Diesel, Paul Walker, Dwayne Johnson, Jordana Brewster, Tyrese Gibson, Elsa Pataky" director="Justin Lin" language="ENGLISH" subtitle="BAHASA INDONESIA" />
</movies>
I have put a breakpoint on the line that creates new Movies. I can confirm variable resultdoc is populated. I tried in the immediate window resultdoc.Descendants("movie").Count returned 15. resultdoc.Descendants("movie").First.Attribute("code").Value correctly returned "MOV1299".
I got this error when I continued from the breakpoint:
System.NullReferenceException was unhandled
Message=NullReferenceException
StackTrace:
at MyApp.MainPageVM.VB$StateMachine_2_GetMovies.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.<>c__DisplayClass5.<OnCompletedInternal>b__1(Object state)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
Why the variable movie contains nothing? How do I make it contain the proper XElement from resultdoc.Descendants("movie")?
[Copied down from the comment]
Go to the Exceptions dialog box (should be under the debug menu) and select to break when NullReferenceException is thrown. You will see exactly where the error is. What may be happening is that one of the XML nodes you are getting back from the server does not have a title or cast or one of the other attribute on them and you are dying on that