❔ 2 Errors in image downloading
My code:
Error string is
pictureBox1.Image.Save(saveFileDialog1.FileName);
, Warning name:
Dereferencing a likely null reference.
And 1 more error - the non-nullable "graphics" field must contain a non-null value when the constructor exits. Might be worth declaring the field as nullable, the second warning code is on image22 Replies
and to be more precise, after clicking on the warning, you are transferred to this line
(On form)
The first error means something there might be null. See which of those values are nullable and guard against that properly
Where is the warning on that line? Either
pictureBox1
, or pictureBox1.Image
, or saveFileDialog1
can be nullThe second, well, I'd need to see some more code
But you have some
graphics
field that doesn't have a default value, and you never give it a value in the constructorok
What code do you need?
Well, the field
The class the field is inside of
Although the error can really only mean one thing, so
waait
namespace EvanPaint
{
public partial class Form1 : Form
this is form1 file
And the field it mentions?
I don't understand you what field
graphics?
wait
graphics = Graphics.FromImage(map);
Yeah, so the field
graphics
has a value only after you call SetSize()
Before you call this method, it'll be undefinedoh
wait
What i need to do
You could give it a default value, and/or shut up the compiler about this error. Since it's a private field, you don't really risk accessing it when uninitialized
Graphics graphics = default;
would be one way, to give it a default value
Graphics graphics = new();
would be another
Graphics graphics = default!;
if it still complainsDo I need to edit private class on public class?
What?
You need to edit this particular class to give this particular field a default value
my error is that what i can't save image in imagebox with button. Program showing savedialog but without saving file, i think warnings are same with this error
Well, then, the issue is probably about something being
null
So the first error
Fire up the debugger and see what might be null
that shouldn't bei launched file with debbuging, all is working, but not save button
Maybe the error is that there are empty buttons without code, or buttons that have an action created and are not inside the code?
i think itš null
and here is this string:
null
if pictureBox image == null - save this image
Hey @Angius! Thank you! I deleted string with code
if(pictureBox1.Image == null) {}
and code now working!!!Nice
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.