0

In my start function, I am setting a variable, but in other functions, it is null... Here is my code where I set the variable:

        string Json;
        string IPV4 = GetIPAddress();
        Debug.Log(IPV4);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip-api.com/json/" + IPV4);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                Json = reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                // log errorText
            }
            throw;
        }
        var ipRequestData = IpRequestData.FromJson(Json);
        Weather = new WeatherData(ipRequestData.City);
        Debug.Log(Weather.City);

The Debug.Log(Weather.City) works and returns my city, but in this next function, it says that Weather is null.

    public List<string> GetTemperature()
    {
        Debug.Log(Weather.City);
        Weather.CheckWeather();
        List<string> WeatherList = new List<string>();
        WeatherList.Add(Weather.Temp.ToString());
        WeatherList.Add(Weather.TempMin.ToString());
        WeatherList.Add(Weather.TempMax.ToString());
        return WeatherList;
    }

I also at the beginning of my class made the variable like this:

WeatherData Weather;

For the WeatherData class, here is my code:

class WeatherData
{
    public WeatherData(string City)
    {
        city = City;
    }
    private string city;
    private float temp;
    private float tempMax;
    private float tempMin;

    public void CheckWeather()
    {
        WeatherAPI DataAPI = new WeatherAPI(City);
        temp = DataAPI.GetTemp();
        SunRise = DataAPI.GetSunrise();
        SunSet = DataAPI.GetSunset();
    }

    public string City { get => city; set => city = value; }
    public float Temp { get => temp; set => temp = value; }
    public float TempMax { get => tempMax; set => tempMax = value; }
    public float TempMin { get => tempMin; set => tempMin = value; }

    public string SunRise;
    public string SunSet;
}
class WeatherAPI
{
    public WeatherAPI(string city)
    {
        SetCurrentURL(city);
        xmlDocument = GetXML(CurrentURL);
    }

    public float GetTemp()
    {
        XmlNode temp_node = xmlDocument.SelectSingleNode("//temperature");
        XmlAttribute temp_value = temp_node.Attributes["value"];
        string temp_string = temp_value.Value;
        return float.Parse(temp_string);
    }
    public string GetSunrise()
    {
        XmlNode temp_node = xmlDocument.SelectSingleNode("//sun");
        XmlAttribute time = temp_node.Attributes["rise"];
        string time_string = time.Value;
        return time_string;
    }
    public string GetSunset()
    {
        XmlNode temp_node = xmlDocument.SelectSingleNode("//sun");
        XmlAttribute time = temp_node.Attributes["set"];
        string time_string = time.Value;
        return time_string;
    }

    private const string APIKEY = "MY API KEY GOES HERE :)";
    private string CurrentURL;
    private XmlDocument xmlDocument;

    private void SetCurrentURL(string location)
    {
        CurrentURL = "http://api.openweathermap.org/data/2.5/weather?q="
            + location + "&mode=xml&units=metric&APPID=" + APIKEY;
    }

    private XmlDocument GetXML(string CurrentURL)
    {
        using (WebClient client = new WebClient())
        {
            string xmlContent = client.DownloadString(CurrentURL);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlContent);
            return xmlDocument;
        }
    }
}


If anyone could help me with this, It would mean a lot to me. Thanks!

Sol
  • 21
  • 1
  • 5
  • Theres a lot of unnecessary code in there... What "other" script says weather is null, how have you referenced the instance of the first script – BugFinder Dec 18 '19 at 21:47
  • Is this in a controller somewhere? Class / instance variables aren't persisted across separate requests, generally speaking. – Nikki9696 Dec 18 '19 at 21:47
  • When are you calling `GetTemperature()`? Have you tried calling it within `Start()`? – jmalenfant Dec 18 '19 at 21:48
  • I'm running GetTemperature() in a coroutine from another script that then uses all the data and updates some text for me. – Sol Dec 19 '19 at 08:01
  • I also read What Is A NullReferenceException, and this didn't help me at all... – Sol Dec 19 '19 at 17:56

0 Answers0