Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a library with AccuWeather API as source, but when i use the getConditions function it gives a "Object not set to a reference" error at "xel.Element("weathertext").Value"

AccuWeather API URL :http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=dinteloord&metric=1&langId=1[^]

VB
 Imports System.Net
Imports System.IO
Imports System.Xml
Imports System.Text
 
Public Class WeatherClient
#Region "Enums"
    Enum Language
        Dutch
        English
    End Enum
    Enum TemperatureScale
        Celcius
        Fahrenheit
    End Enum
#End Region
#Region "Constructor"
    Dim tmpscale As TemperatureScale
    Dim lang As Language
 
    Public Sub New(ByVal cityname As String, ByVal language As Language, tempscale As TemperatureScale)
        locat = cityname
        lang = language
        tmpscale = tempscale
        getconditions()
    End Sub
#End Region
#Region "Inner classes"
    Public Class CurrentConditions
 
        Dim cond As String
        Dim tmp As String
        Dim wnd As String
        Dim wnddir As String
        Dim rlfl As String
        Dim humid As Integer
        Dim visib As Integer
        Dim precip As Integer
        Dim uvindx As String
        Dim dwpnt As Integer
        Dim clcover As Integer
        Dim wchill As Integer
        Public ReadOnly Property Condition As String
            Get
                Return cond
            End Get
        End Property
        Public ReadOnly Property Temperature As String
            Get
                Return tmp
            End Get
        End Property
        Public ReadOnly Property WindSpeed As String
            Get
                Return wnd
            End Get
        End Property
        Public ReadOnly Property WindDirection As String
            Get
                Return wnddir
            End Get
        End Property
        Public ReadOnly Property RealFeal As String
            Get
                Return rlfl
            End Get
        End Property
        Public ReadOnly Property Humidity As Integer
            Get
                Return humid
            End Get
        End Property
        Public ReadOnly Property Visibility As Integer
            Get
                Return visib
            End Get
        End Property
        Public ReadOnly Property Precipitation As Integer
            Get
                Return precip
            End Get
        End Property
        Public ReadOnly Property UVIndex As String
            Get
                Return uvindx
            End Get
        End Property
        Public ReadOnly Property DewPoint As Integer
            Get
                Return dwpnt
            End Get
        End Property
        Public ReadOnly Property CloudCover As Integer
            Get
                Return clcover
            End Get
        End Property
        Public ReadOnly Property WindChill As Integer
            Get
                Return wchill
            End Get
        End Property
        Public Sub New(ByVal cnd As String, tmp2 As String, wnd2 As String, wnddir2 As String, rld As String, hum As String, vis As String, prec As String, uvn As String, dwn As String, clc As String, wc As String)
            cond = cnd
            tmp = tmp2
            wnd = wnd2
            wnddir = wnddir2
            rlfl = rld
            humid = hum
            visib = vis
            precip = prec
            uvindx = uvn
            dwpnt = dwn
            clcover = clc
            wchill = wc
        End Sub
    End Class
    Public Class ForecastCondition
 
        Dim dh As String
        Dim dl As String
        Dim drh As String
        Dim drl As String
        Dim ddwnspd As String
        Dim ddwndir As String
        Dim dramound As String
        Dim dsamound As String
        Dim diamound As String
        Dim dpamount As String
        Dim dtstormamount As String
        Dim nh As String
        Dim nl As String
        Dim nrh As String
        Dim nrl As String
        Dim ndwnspd As String
        Dim ndwndir As String
        Dim nramound As String
        Dim nsamound As String
        Dim niamound As String
        Dim npamount As String
        Dim ntstormamount As String
        Dim dncd As String
        Dim ncnd As String
        Dim daycod As String
        Public ReadOnly Property Day As String
            Get
                Return daycod
            End Get
        End Property
        Public ReadOnly Property DayTime As DayTimer
            Get
                Return New DayTimer(dh, dl, drh, drl, ddwnspd, ddwndir, dramound, dsamound, diamound, dpamount, dtstormamount, dncd)
            End Get
        End Property
        Public ReadOnly Property NightTime As NightTimer
            Get
                Return New NightTimer(nh, nl, nrh, nrl, ndwnspd, ndwndir, nramound, nsamound, niamound, npamount, ntstormamount, ncnd)
            End Get
        End Property
        Public Sub New(ddh As String, dll As String, ddrh As String, ddrl As String, dddwnspd As String, dddwndir As String, ddramound As String, ddsamound As String, ddiamound As String, ddpamount As String, ddta As String _
                       , nnh As String, nnl As String, ndrh As String, ndrl As String, nwndspd As String, nwnddir As String, ndramount As String, ndsamound As String, ndiamound As String, ndpamound As String, ndta As String _
                       , dayc As String, dcond As String, ncond As String)
            dh = ddh
            dl = dll
            drh = ddrh
            drl = ddrl
            ddwnspd = dddwnspd
            ddwndir = dddwndir
            dramound = ddramound
            dsamound = ddsamound
            diamound = ddiamound
            dpamount = ddpamount
            dtstormamount = ddta
            nh = nnh
            nl = nnl
            nrh = ndrh
            nrl = ndrl
            ndwnspd = nwndspd
            ndwndir = nwnddir
            nramound = ndramount
            nsamound = ndsamound
            niamound = ndiamound
            npamount = ndpamound
            ntstormamount = ndta
            daycod = dayc
            dncd = dcond
            ncnd = ncond
        End Sub
        Class DayTimer
            Dim h As String
            Dim l As String
            Dim rh As String
            Dim rl As String
            Dim dwnspd As String
            Dim dwndir As String
            Dim ramound As String
            Dim samound As String
            Dim iamound As String
            Dim pamound As String
            Dim tstormamound As String
            Dim cond As String
            Public ReadOnly Property Condition As String
                Get
                    Return cond
                End Get
            End Property
            Public ReadOnly Property High As String
                Get
                    Return h
                End Get
            End Property
            Public ReadOnly Property Low As String
                Get
                    Return l
                End Get
            End Property
            Public ReadOnly Property RealFeelHigh As String
                Get
                    Return rh
                End Get
            End Property
            Public ReadOnly Property WindSpeed As String
                Get
                    Return dwnspd
                End Get
            End Property
            Public ReadOnly Property WindDirection As String
                Get
                    Return dwndir
                End Get
            End Property
            Public ReadOnly Property RainAmount As String
                Get
                    Return ramound
                End Get
            End Property
            Public ReadOnly Property SnowAmount As String
                Get
                    Return samound
                End Get
            End Property
            Public ReadOnly Property IceAmount As String
                Get
                    Return iamound
                End Get
            End Property
            Public ReadOnly Property Precipitation As String
                Get
                    Return pamound
                End Get
            End Property
            Public ReadOnly Property ThunderstormPropebility As String
                Get
                    Return tstormamound
                End Get
            End Property
            Public Sub New(hi As String, lo As String, rhi As String, hlo As String, wndspd As String, wnddir As String, r As String, s As String, i As String, p As String, ta As String, cnd As String)
                h = hi
                l = lo
                rh = rhi
                rl = hlo
                dwnspd = wndspd
                dwndir = wnddir
                ramound = r
                samound = s
                iamound = i
                pamound = p
                tstormamound = ta
                cond = cnd
            End Sub
        End Class
        Class NightTimer
 
            Dim h As String
            Dim l As String
            Dim rh As String
            Dim rl As String
            Dim dwnspd As String
            Dim dwndir As String
            Dim ramound As String
            Dim samound As String
            Dim iamound As String
            Dim pamound As String
            Dim tstormamound As String
            Dim cond As String
            Public ReadOnly Property Condition As String
                Get
                    Return cond
                End Get
            End Property
            Public ReadOnly Property High As String
                Get
                    Return h
                End Get
            End Property
            Public ReadOnly Property Low As String
                Get
                    Return l
                End Get
            End Property
            Public ReadOnly Property RealFeelHigh As String
                Get
                    Return rh
                End Get
            End Property
            Public ReadOnly Property WindSpeed As String
                Get
                    Return dwnspd
                End Get
            End Property
            Public ReadOnly Property WindDirection As String
                Get
                    Return dwndir
                End Get
            End Property
            Public ReadOnly Property RainAmount As String
                Get
                    Return ramound
                End Get
            End Property
            Public ReadOnly Property SnowAmount As String
                Get
                    Return samound
                End Get
            End Property
            Public ReadOnly Property IceAmount As String
                Get
                    Return iamound
                End Get
            End Property
            Public ReadOnly Property Precipitation As String
                Get
                    Return pamound
                End Get
            End Property
            Public ReadOnly Property ThunderstormPropebility As String
                Get
                    Return tstormamound
                End Get
            End Property
            Public Sub New(hi As String, lo As String, rhi As String, hlo As String, wndspd As String, wnddir As String, r As String, s As String, i As String, p As String, ta As String, cnd As String)
                h = hi
                l = lo
                rh = rhi
                rl = hlo
                dwnspd = wndspd
                dwndir = wnddir
                ramound = r
                samound = s
                iamound = i
                pamound = p
                tstormamound = ta
                cond = cnd
            End Sub
        End Class
    End Class
 
    Public Class ForecastCollection
        Inherits CollectionBase
        Public Overridable Sub Add(ByVal frday As ForecastCondition)
            Me.List.Add(frday)
        End Sub
    End Class
#End Region
 
#Region "Properties"
    Dim locat As String
    Dim cond As String
    Dim tmp As String
    Dim wnd As String
    Dim wnddir As String
    Dim rlfl As String
    Dim humid As Integer
    Dim visib As Integer
    Dim precip As Integer
    Dim uvindx As String
    Dim dwpnt As Integer
    Dim clcover As Integer
    Dim wchill As Integer
    Dim fconditionlist As New ForecastCollection
    Public ReadOnly Property Current As CurrentConditions
        Get
            Return New CurrentConditions(cond, tmp, wnd, wnddir, rlfl, humid, visib, precip, uvindx, dwpnt, clcover, wchill)
        End Get
    End Property
    Public ReadOnly Property Forecast As ForecastCollection
        Get
            Return fconditionlist
        End Get
    End Property
#End Region
#Region "Functions"
    Private Function getconditions()
        Dim wbreq As WebRequest
        Dim wbresp As WebResponse
        Select Case lang
            Case Language.Dutch
                Select Case tmpscale
                    Case TemperatureScale.Celcius
                        wbreq = WebRequest.Create("http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=" & locat & "&metric=1&langId=6")
                        wbresp = wbreq.GetResponse
                    Case TemperatureScale.Fahrenheit
                        wbreq = WebRequest.Create("http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=" & locat & "&metric=0&langId=6")
                        wbresp = wbreq.GetResponse
                End Select
            Case Language.English
                Select Case tmpscale
                    Case TemperatureScale.Celcius
                        wbreq = WebRequest.Create("http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=" & locat & "&metric=1&langId=1")
                        wbresp = wbreq.GetResponse
                    Case TemperatureScale.Fahrenheit
                        wbreq = WebRequest.Create("http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=" & locat & "&metric=0&langId=1")
                        wbresp = wbreq.GetResponse
                End Select
        End Select
 
        Dim strreader As New StreamReader(wbresp.GetResponseStream, Encoding.UTF8)
        Dim xml As String = strreader.ReadToEnd
        Dim xdoc As XDocument = XDocument.Parse(xml)
        Dim xel As XElement = xdoc.Root.Element(XName.Get("currentconditions", xdoc.Root.Name.Namespace.NamespaceName))
        cond = xel.Element("weathertext").Value
        tmp = xel.Element("temperature").Value
        wnd = xel.Element("windspeed").Value
        wnddir = xel.Element("winddirection").Value
 
        rlfl = xel.Element("realfeel").Value
        humid = xel.Element("humidity").Value
        visib = xel.Element("visibility").Value
        precip = xel.Element("precip").Value
        uvindx = xel.Element("uvindex").Attribute("index").Value & " - " & xdoc.Element("uvindex").Value
        dwpnt = xel.Element("dewpoint").Value
        clcover = xel.Element("cloudcover").Value
        wchill = xel.Element("windchill").Value
        For Each nod As XElement In xdoc.Root.Element(XName.Get("forecast", xdoc.Root.Name.Namespace.NamespaceName)).Elements()
            Dim dh As Integer
            Dim dl As Integer
            Dim drh As Integer
            Dim drl As Integer
            Dim ddwnspd As Integer
            Dim ddwndir As String
            Dim dramound As Integer
            Dim dsamound As Integer
            Dim diamound As Integer
            Dim dpamount As Integer
            Dim dtstormamount As Integer
            Dim nh As Integer
            Dim nl As Integer
            Dim nrh As Integer
            Dim nrl As Integer
            Dim ndwnspd As Integer
            Dim ndwndir As String
            Dim nramound As Integer
            Dim nsamound As Integer
            Dim niamound As Integer
            Dim npamount As Integer
            Dim ntstormamount As Integer
            Dim daycod As String
            Dim daycnd As String
            Dim ncnd As String
            Dim dt As XElement = nod.Element("daytime")
            dh = dt.Element("hightemperature").Value
            dl = dt.Element("lowtemperature").Value
            drh = dt.Element("realfeelhigh").Value
            drl = dt.Element("realfeellow").Value
            ddwnspd = dt.Element("windspeed").Value
            ddwndir = dt.Element("winddirection").Value
            dramound = dt.Element("rainamount").Value
            dsamound = dt.Element("snowamount").Value
            diamound = dt.Element("iceamount").Value
            dpamount = dt.Element("precipamount").Value
            dtstormamount = dt.Element("tstormprob").Value
            daycnd = dt.Element("txtshort").Value
            Dim nt As XElement = nod.Element("nighttime")
            nh = nt.Element("hightemperature").Value
            nl = nt.Element("lowtemperature").Value
            nrh = nt.Element("realfeelhigh").Value
            nrl = nt.Element("realfeellow").Value
            ndwnspd = nt.Element("windspeed").Value
            ndwndir = nt.Element("winddirection").Value
            nramound = nt.Element("rainamount").Value
            nsamound = nt.Element("snowamount").Value
            niamound = nt.Element("iceamount").Value
            npamount = nt.Element("precipamount").Value
            ntstormamount = nt.Element("tstormamount").Value
            ncnd = nt.Element("txtshort").Value
            daycod = nod.Element("daycode").Value
            fconditionlist.Add(New ForecastCondition(dh, dl, drh, drl, ddwnspd, ddwndir, dramound, dsamound, diamound, dpamount, dtstormamount, nh, nl, nrh, nrl, ndwnspd, ndwndir, nramound, nsamound, niamound, npamount, ntstormamount, daycod, daycnd, ncnd))
        Next
    End Function
#End Region
 
End Class 
Posted
Updated 16-Jan-17 0:32am
v2

change Xel.Element("wheathertext").value with xel.elements.elementAt(Index).value
 
Share this answer
 
Just add following reference.:-
xmlConditions.SelectSingleNode("xml_api_reply/weather/problem_cause")
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900