locked
Object Reference Not Set To An Instance of an Object: .Get returned Nothing RRS feed

  • Question

  • Hello,

    I have a weather application that I am having issued getting forecasts. I am getting the following Null Reference Exception:

    System.NullReferenceException
      HResult=0x80004003
      Message=Object reference not set to an instance of an object.
      Source=AWNHD Weather Broadcast Engine
      StackTrace:
       at AWNHD_Weather_Broadcast_Engine.WxDisplay.NWS_Forecast() in C:\Users\aawea\source\repos\AWNHD Weather Broadcast Engine\WxDisplay.vb:line 199

    Here is the code in the form:

    Using WB As New WebClient
                Try
                    WB.Headers.Add("User-Agent", "AWN WIGC Engine HD/v1.0 (http://www.austinlandweather.com; EMAIL")
                    Dim ForecastData As String = WB.DownloadString($"https://api.weather.gov/zones/forecast/{My.Settings.NWS_Wx_Forecast}/forecast")
    
                    Dim weather As WeatherData = JsonConvert.DeserializeObject(Of WeatherData)(ForecastData)
    
                    Dim period As Period = New Period
    
                    Period1Forecast.Text = weather.periods(0).detailedForecast
    Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End Using
    
    
    

    The exception says AWNHD_Weather_Broadcast_Engine.WeatherData.periods.get returned Nothing. Here is the code for WeatherData.periods:

    Public Class Period
        Public Property number As Integer
        Public Property name As String
        Public Property detailedForecast As String
    End Class

    My application is downloading the string for the json feed but is failing to deserialize or is failing to take the information and display it in a label. I tried a textbox and I get the same result.

    This code ran smoothly for several months and randomly quit working one day. I am running VS 2019. How can that error be corrected and why is it occurring?

    Friday, July 31, 2020 5:52 PM

All replies

  • Hi

    I don't think this is the cause of the error, but you have defined a variable as a New Period but not used it (as far as the code goes anyway).

    If you do not have Option Strict On at the  top of your code, please add it and see if that points out possible errors waiying to happen.

    As your Try...Catch didn't fire, then the exception must be elsewhere.  Put a Break Point on the WB.Headers.Add line and single step from there and see where if crashes out.


    Regards Les, Livingston, Scotland

    Friday, July 31, 2020 6:14 PM
  • Did you try the code I last post in this thread?

    Imports System.IO
    Imports Newtonsoft.Json
    Imports Weatherjson1.WeatherJson1
    
    Public Class Form1
        Private Sub ReadJsonFromFileButton_Click(sender As Object, e As EventArgs) _
            Handles ReadJsonFromFileButton.Click
    
            '
            ' In your code this comes from the download string
            '
            Dim weather As Root = JsonConvert.DeserializeObject(Of Root)(File.ReadAllText(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "weather.json")))
    
            If weather.Properties.periods.Count > 0 Then
                DataGridView1.DataSource = weather.Properties.periods
    
                '
                ' If we don't clone then moving position in the DataGridView
                ' move position in the ComboBox and reverse is true
                '
                Dim cloneList = New List(Of Period)
                cloneList.AddRange(weather.Properties.periods)
                PeriodComboBox.DataSource = cloneList
    
            Else
                PeriodComboBox.Enabled = False
            End If
    
        End Sub
    
        Private Sub PeriodComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
            Handles PeriodComboBox.SelectedIndexChanged
    
            Dim selectedPeriod = CType(PeriodComboBox.SelectedItem, Period)
            Label1.Text = selectedPeriod.detailedForecast
    
        End Sub
    End Class
    Namespace WeatherJson1
        Public Class Context
            Public Property version() As String
    
        End Class
    
        Public Class Geometry
            Public Property type() As String
            Public Property coordinates() As List(Of List(Of List(Of List(Of Double))))
        End Class
    
        Public Class Period
            Public Property number() As Integer
            Public Property name() As String
            Public Property detailedForecast() As String
    
            Public Overrides Function ToString() As String
                Return name
            End Function
        End Class
    
        Public Class Root
            Public Property context() As Context
            Public Property type() As String
            Public Property geometry() As Geometry
            Public Property Properties() As Properties
    
        End Class
        Public Class Properties
            Public Property zone() As String
            Public Property updated() As DateTime
            Public Property periods() As List(Of Period)
    
        End Class
    End Namespace


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 31, 2020 6:24 PM
  • Karen,

    I was able to get your code to work but I am still getting the object reference not set to an instance of and object error. Why would it still be doing this? Could something be missing in my version of visual studio?

    Thanks so much for your assistance!



    Friday, July 31, 2020 6:46 PM
  • Are you indicting my code also gets object not set to an instance error or not ?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 31, 2020 8:51 PM
  • Karen,

    Yes, I tried your code on my application and it to got a instance error. I don't know what is going on with it.

    Friday, July 31, 2020 9:09 PM
  • What version of the .NET Framework is being used? I generally use 4.7.2

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 31, 2020 10:24 PM
  • Karen,

    The target version in my application for the .NET framework is 4.7.2.


    Friday, July 31, 2020 10:49 PM
  • Last thought is this has something to do with a firewall or virus protection.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 31, 2020 11:17 PM
  • Last thought is this has something to do with a firewall or virus protection.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 31, 2020 11:18 PM
  • Karen,

    I tried and uninstalled the antivirus I had on my computer and still no positive results. Thank you very much for trying to help!

    Friday, July 31, 2020 11:25 PM
  • Hi Austin Althouse,

    In order to help us analyze and reproduce your problem, could you provide some related code about 'NWS_Wx_Forecast' and 'WeatherData' ?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 3, 2020 7:14 AM