C
C#14mo ago
Genr1se

❔ Converting null iteral or possible null value to non-nullable type

I'm getting this hint:
Converting null iteral or possible null value to non-nullable type
Converting null iteral or possible null value to non-nullable type
Do I need to do something about it?
59 Replies
Pobiega
Pobiega14mo ago
need is a strong word, but you probably should it means you are not respecting null annotations ie string name = MethodThatCanReturnNull(); string name is not string? name we're saying name definately has a value but since MethodThatCanReturnNull returns a string? you get a warning
Genr1se
Genr1seOP14mo ago
So what should I do? I'm new to C#
Pobiega
Pobiega14mo ago
can you show the code in question?
Genr1se
Genr1seOP14mo ago
Give me a second
Pobiega
Pobiega14mo ago
but in general, you'd check for null and handle it or use a nullable type on the left side too Do you understand the concept of null and why we must handle it?
Genr1se
Genr1seOP14mo ago
Pobiega
Pobiega14mo ago
right
strServer = srIni.ReadLine();
strUser = srIni.ReadLine();
strPass = srIni.ReadLine();
strMailOK = srIni.ReadLine();
strMailFailure = srIni.ReadLine();
strDirectory = @srIni.ReadLine();
strServer = srIni.ReadLine();
strUser = srIni.ReadLine();
strPass = srIni.ReadLine();
strMailOK = srIni.ReadLine();
strMailFailure = srIni.ReadLine();
strDirectory = @srIni.ReadLine();
Genr1se
Genr1seOP14mo ago
Because it can cause errors otherwise I guess
Pobiega
Pobiega14mo ago
exactly Im assuming srIni.ReadLine() returns string? and not string
Genr1se
Genr1seOP14mo ago
It's a line from an .ini file
Pobiega
Pobiega14mo ago
I get that. in general, that code is very error prone. IndexOf might return -1 (if the needle wasnt found)
Genr1se
Genr1seOP14mo ago
It is, but doesn't matter in my case What should I do about the null hint tho?
Pobiega
Pobiega14mo ago
I mean, if you are going to say "yolo i dont care about potential errors", then nothing its a warning that you have a potential error just like I informed you that indexof can return -1 thats a potential error
Genr1se
Genr1seOP14mo ago
It wasn't handled before. I'm just rewriting a part of the code So I was wondering if I have to do something about it
Pobiega
Pobiega14mo ago
Well, what do you want to do? Ignore it, or deal with it?
Genr1se
Genr1seOP14mo ago
Fix it, but idk how
Pobiega
Pobiega14mo ago
assuming your ini file isnt broken, you can safely ignore it but if there is a chance someone fucks up the ini file, it'd be better to be safe and handle it
Genr1se
Genr1seOP14mo ago
How would you handle it? With a try and catch around it? Because that's already there
Pobiega
Pobiega14mo ago
nah First step is deciding how to handle it, as in, "can I recover from this?" lets say that when you try to read the strServer it returns null can you recover from that? would your program still work without a value for strServer, or can we default it? if recovery is impossible, its best to throw an exception or otherwise terminate the method
Genr1se
Genr1seOP14mo ago
You mean if the main purpose of the program could still be fulfilled or if everything would work?
Pobiega
Pobiega14mo ago
yeah kinda maybe not "main purpose" but "this piece of codes purpose" if strServer is null, we can't try to split it, that will crash
Genr1se
Genr1seOP14mo ago
Without the strServer it wouldn't
Pobiega
Pobiega14mo ago
right then I suggest you add some null or throw. example
strServer = srIni.ReadLine() ?? throw new Exception("Unable to read server from ini file");
strServer = srIni.ReadLine() ?? throw new Exception("Unable to read server from ini file");
this will read the value, and if the value was null, it throws an exception
Genr1se
Genr1seOP14mo ago
Is ?? saying if null?
Pobiega
Pobiega14mo ago
yep if the value on the left is null, use the right instead
Genr1se
Genr1seOP14mo ago
Should I do the same for the other variables?
Pobiega
Pobiega14mo ago
yes
Genr1se
Genr1seOP14mo ago
Is it okay to throw so many new Exceptions?
Pobiega
Pobiega14mo ago
its not happening unless the value was null and only one will ever throw as throwing will stop the rest from executing
Genr1se
Genr1seOP14mo ago
Got it And then it's fixed already?
Pobiega
Pobiega14mo ago
the only downside here is that it looks a bit nasty but you could fix this with putting it in a method or an extension method plenty of ways to solve it 🙂
Genr1se
Genr1seOP14mo ago
So do ?? ThrowExceptionMethod()
Pobiega
Pobiega14mo ago
no I was thinking more...
strServer = srIni.ReadLine().OrThrow("Unable to read server from ini");
strServer = srIni.ReadLine().OrThrow("Unable to read server from ini");
Genr1se
Genr1seOP14mo ago
How does that work
Pobiega
Pobiega14mo ago
you'd make an extension method that checks the value for null or throws eh, not sure its a great idea now that I think about it its less obvious what it does
Genr1se
Genr1seOP14mo ago
So the way I planned would be better?
Pobiega
Pobiega14mo ago
?
Genr1se
Genr1seOP14mo ago
I mean this way
Pobiega
Pobiega14mo ago
strServer = srIni.ReadLine() ?? throw new Exception("Unable to read server from ini file"); imho
Genr1se
Genr1seOP14mo ago
Alright then I'mma do that
Pobiega
Pobiega14mo ago
if you just slap a method for the throw you gain nothing
Genr1se
Genr1seOP14mo ago
But what's the difference between this and the try and catch that's already around it?
Pobiega
Pobiega14mo ago
a try catch catches exceptions. what we are doing is converting a possible NullReferenceException into another exception with a much better message so that its easier to find the problem if we dont throw on null here, the program WILL throw an NRE
Genr1se
Genr1seOP14mo ago
Aah so this will be caught by the try and catch then?
Pobiega
Pobiega14mo ago
yes
Genr1se
Genr1seOP14mo ago
That is awesome
Pobiega
Pobiega14mo ago
but NREs have terrible error messages so we throw a better exception
Genr1se
Genr1seOP14mo ago
Great, that also got rid of the hint
Pobiega
Pobiega14mo ago
yep since you are handling the potential error 🙂
Genr1se
Genr1seOP14mo ago
PepeHappy should I also do it for the mail address from the ini file? Because the program could still run without it if there is no other error Like for strMailFailure and strMailOK
Pobiega
Pobiega14mo ago
You'll need to handle those differently then
Genr1se
Genr1seOP14mo ago
How would I do that?
Pobiega
Pobiega14mo ago
I'm sure you can figure it out
Genr1se
Genr1seOP14mo ago
There is already a try and catch with a throw around the part where the mail is sent That should be fine right?
Pobiega
Pobiega14mo ago
Impossible to answer. It stops an exception from bubbling up, but what happens inside your code and it's state is... well, only you can answer that Coding really isnt as easy as just wrapping a trycatch around everything 🙂
Genr1se
Genr1seOP14mo ago
It looks good to me, so I'll leave it Do you know by any chance what
client.BufferSize = 4*1024
client.BufferSize = 4*1024
does when client is an instance of SftpClient?
Pobiega
Pobiega14mo ago
It sets the buffer size to be 4 kb
Genr1se
Genr1seOP14mo ago
Which means what? The comment says "bypass payload error large files"
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server