β Null warning from accessing weather results from an API call
I'm creating a weather app that allows you to type in the name of a city and then get it's information from which you can use a key to get the weather for it. I've gotten the first part down, but I'm having trouble with the second part in that I try to get the JSON results from a different api link where I need the city key. I keep getting a null warning from my code when I try to get and deserialize the json, but I don't see anything in the JSON itself that has a null value. any suggestions?
pastebin of code:
https://paste.mod.gg/miyqeovcslff/0
pic of json:
BlazeBin - miyqeovcslff
A tool for sharing your source code with the world!
101 Replies
depends how you're deserialzing it
i'm trying to serialize it into an object
show code...
in the blazebin
or i can paste it here
1 sec
nah link is good
ok
so what is null? weatherResult?
yeah, let me run the code again real quick and sc it
null warnings usually just say that if you put null into the field it will throw exception so you can put a null reference checker (either ?? or if statement) and it should do
assuming that codewise nothing is crashing or not working correctly
What is the exact 'null warning' that you get?
gimme a sec my code that was working all this time isn't working anymore
System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 503 (Unauthorized).'
i'm now getting this when i run my program at this line of code
// Get JSON results and deserialize
var citySearch = await client.GetFromJsonAsync<List<Root>>(link);
authorize
maybe your apikey is rate limited
aw man no way
i forgot about that
so it says 50 calls a day
so if i've run my program 50 times that means all my calls are gone?
yup
NOOOOO
hold on im gonna get another key
suggest you then save the json data to local file π
oh yeah i never thought about that haha
here it is
oh i see, so i can bypass the exception and write out whatever the value is still?
but if its null wouldn't it just be nothing
no here it throws an exception not a warning
oh my bad, i said it was a null warning
i thought they were the same thing
weatherResult being null means the json doesnt match the binding class -
DailyForecast
oh so there IS something wrong with my property class
ok i'll look into that some more
you can paste the json here,https://quicktype.io/, or directly into visual studio, to get c# classes to match the json
The warning is the compiler telling you "Hey, this could be null but it prob'ly shouldn't be," warning you that there could be an exception. The exception is the runtime saying "This is null and I don't like it"
yeah i got one, someone recommended json2csharp.com
i'm following the format, but i guess i did something wrong
ah ok, so the warning leads to the exception
It just means that there could be an exception
oh i see what i did wrong now
i completely forgot there was another sub-property in maximum and minimum π€¦ββοΈ
wait im not sure anymore
this is what the documentation says
and this is what the converter recommends based off the json file
this DOES mean that i need 2 more classes for minimum and maximum right?
The exception makes it sound like it's the Temperature object that didn't get deserialized correctly. Did the date print out correctly when you ran it?
yeah i think im starting to get it
i need to struggle with it a little more
but yeah i think the temperature object is the problem
The first thing I would try is changing the Value properties on
Minimum
and Maximum
(realistically they should be the same class) to double
instead of int
. The values in the Json have decimal points so idk why the converter recommends int
.i'm stumped, i thought i had it but i guess i didn't
i've been trying to figure this out for a while but i just cant seem to get it right
i changed up my classes to reflect the documentation and the json class converter, but i can't access any of the results
i keep getting this error in the code
but in the documentation the api i'm calling says it returns an array, and i was able to turn the json into a list previously
I've tried using var weatherResult = await client.GetStringAsync(link);
and printing out weatherResult, but then my program won't even run due to build errors
I see you removed the
&q={cityInput}
from your link and the int UnitType
from the Minimum and Maximum classes. Is that right?well i plan on adding back cityInput later when i get the json deserialization and printing all correct
and i did remove the int UnitType
Ah okay, so the API still returns a json if you don't provide a city?
i removed stuff i didn't want from the json
so in this link http://dataservice.accuweather.com/forecasts/v1/daily/5day/2178895?apikey=
the 2178895 is the location key i'm able to get from
the api i'm using (accuweather) has a flowchart where it says to get the city key from the location api
and then use that city key to call the forecast api where i can get the weather
the current flow of my program is able to get the city's general information as seen in the foreach loop
and since there are multiple cities with the same name in different countries, i'll have the user enter in their desired city key which will then output all the weather
but im having trouble with accessing the weather in the first palce
i thought it would be as simple as this code i just posted since in the documentation it says they both return an array
my current thought process is that my problem is stemming from my newRoot class, where it's a list of the DailyForecast type called DailyForecasts
honestly idk, im just stumped
I'd start with just trying to get the json string from the api into the console as text to make sure you're calling the api correctly. Once you have that working we can focuus on deserializing them
and i would do that through GetStringAsync right?
when i try to run this code, my app won't even run at all, saying there were build errors
What are the build errors?
nevermind im dumb
i didn't see that i didnt comment out a curly brace
yup im able to get the json
this doesn't look like an array straight up to me
It looks like there's a
Headline
and then an array of DailyForecast
sdo i need to include headline in my property class?
i was told that i could omit properties that i didn't plan on including in my classes when dealing with apis
You can omit them, yeah. I've only dealt with deserialization with
Newtonsoft.Json
and making my models like this where you specify what each thing is called in the json fileooh i see
i've heard about newtonsoft.json, is it widely used?
maybe i should switch over to that
I think it's one of, if not the most downloaded nuget package
would i run into the same problem i'm having right now even if i used newtson soft though?
surely this is a logic problem im just not understanding
Prob'ly. I was just sharing what I have experience with
fair, definitely gonna look into that later
looking at the json in the formatter, i can see that dailyforecast is indeed an array
and it would seem that my classes are correct according to the converter, also taking into account making changes according to the documentation of the api
What happens if you try to deserialize it now?
so just to make sure,
this line of code makes sense right?
referencing this ^
Shouldn't there be only one newRoot? Or is it giving you multiple in one call?
yeah i only have one newRoot
if i run that code i get this
You're trying to deserialize it into a
List<newRoot>
yup
But there's only one
huh?
im confused
GetFromJsonAsync<List<newRoot>(link)
means you're trying to read an array of newRoot
from the string. But the string is not an array of newRoot
, it's only onei think i get what youre trying to say?
so in this case, when im using <List<newRoot>>: i'm assuming that the entire json ouput is an array
when in reality it isn't entirely an array?
Right. The only array part of the Json is the DailyForecasts, which are part of newRoot
aiya
so then i need to figure out how to deserialize the whole thing
You need to figure out how to derserialize the one thing
and by one thing do you mean the json output?
I mean the newRoot
isn't it not just a matter of removing the <list>?
It is exactly that
then i have the newRoot object that is retrieved and deserialized
NICE
You're only trying to read one newRoot, not a list of them
and again i know that its only ONE newRoot since the entire output isn't an array?
Yeah
got it
so now i just need to output the result
when i run the code with console.WriteLine(weatherResult.DailyForecasts)
i see that it's a list
but when i try printing out the contents in a foreach loop, i can't do so
Do you know how to set breakpoint and debug your variables?
yes and no?
in this case would i set the breakpoint on this line?
and then just hover over it when it reaches that point to see the values?
Since we know that it's not crashing, I'd set the breakpoint after that and inspect your weatherResult variable to see what's in it
got it
yup the values are there
let me try and get a sc
That looks good. You've got 5 DailyForecasts in that array
i'm just confused why in the autocomplete it doesn't give me these options to add to the code
yup the api i'm calling gets the forecast for 5 days
WAIT
do i just need to do
Console.WriteLine(weatherResult.DailyForecasts[0]);
ok amybe not
it didnt work
That'll prob'ly just output
WeatherApp.DailyForecast
oh wait
now the property options are coming up
Mmhmm, cuz now you're accessing the DailyForecast that's in the List
IT WORKS
it completely slipped my mind to access the array values
but obviously i should've since it's an array lol
THANK YOU VERY MUCH
i appreciate all your helpπ€
Np, now you get to do fun stuff with all your weather data~
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.