C
C#2y ago
inexQ

❔ 2 Errors in image downloading

My code:
private void button11_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPG(*.JPG)|*.jpg";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if(pictureBox1.Image == null)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
}
private void button11_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPG(*.JPG)|*.jpg";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if(pictureBox1.Image == null)
{
pictureBox1.Image.Save(saveFileDialog1.FileName);
}
}
}
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 image
22 Replies
inexQ
inexQOP2y ago
and to be more precise, after clicking on the warning, you are transferred to this line (On form)
Angius
Angius2y ago
The first error means something there might be null. See which of those values are nullable and guard against that properly
ero
ero2y ago
Where is the warning on that line? Either pictureBox1, or pictureBox1.Image, or saveFileDialog1 can be null
Angius
Angius2y ago
The 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 constructor
inexQ
inexQOP2y ago
ok What code do you need?
Angius
Angius2y ago
Well, the field The class the field is inside of Although the error can really only mean one thing, so
inexQ
inexQOP2y ago
inexQ
inexQOP2y ago
waait namespace EvanPaint { public partial class Form1 : Form this is form1 file
Angius
Angius2y ago
And the field it mentions?
inexQ
inexQOP2y ago
I don't understand you what field
Angius
Angius2y ago
inexQ
inexQOP2y ago
graphics? wait
private class ArrayPoints
{
private int index = 0;
private Point[] points;
public ArrayPoints(int size)
{
if(size <= 0) { size = 2; }
points = new Point[size];
}
public void SetPoint(int x, int y)
{
if(index >= points.Length)
{
index = 0;
}
points[index] = new Point(x, y);
index++;
}
public void ResetPoints()
{
index = 0;
}
public int GetCountPoints()
{
return index;
}
public Point[] GetPoints()
{
return points;
}
}
private bool isMouse = false;
private ArrayPoints arrayPoints = new ArrayPoints(2);
Bitmap map = new Bitmap(100, 100);
Graphics graphics;
Pen pen = new Pen(Color.Black, 3f);

private void SetSize()
{
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
map = new Bitmap(rectangle.Width, rectangle.Height);
graphics = Graphics.FromImage(map);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
private class ArrayPoints
{
private int index = 0;
private Point[] points;
public ArrayPoints(int size)
{
if(size <= 0) { size = 2; }
points = new Point[size];
}
public void SetPoint(int x, int y)
{
if(index >= points.Length)
{
index = 0;
}
points[index] = new Point(x, y);
index++;
}
public void ResetPoints()
{
index = 0;
}
public int GetCountPoints()
{
return index;
}
public Point[] GetPoints()
{
return points;
}
}
private bool isMouse = false;
private ArrayPoints arrayPoints = new ArrayPoints(2);
Bitmap map = new Bitmap(100, 100);
Graphics graphics;
Pen pen = new Pen(Color.Black, 3f);

private void SetSize()
{
Rectangle rectangle = Screen.PrimaryScreen.Bounds;
map = new Bitmap(rectangle.Width, rectangle.Height);
graphics = Graphics.FromImage(map);
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
graphics = Graphics.FromImage(map);
Angius
Angius2y ago
Yeah, so the field graphics has a value only after you call SetSize() Before you call this method, it'll be undefined
inexQ
inexQOP2y ago
oh wait What i need to do
Angius
Angius2y ago
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 complains
inexQ
inexQOP2y ago
Do I need to edit private class on public class?
Angius
Angius2y ago
What? You need to edit this particular class to give this particular field a default value
inexQ
inexQOP2y ago
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
Angius
Angius2y ago
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 be
inexQ
inexQOP2y ago
i 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:
if(pictureBox1.Image == null)
if(pictureBox1.Image == null)
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!!!
Angius
Angius2y ago
Nice
Accord
Accord2y 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