Note that there are some explanatory texts on larger screens.

plurals
  1. POnullreferenceexception in C# with nested for loop
    primarykey
    data
    text
    <p>while I'm developing an application that does some calculations for the image it raises a nullreferenceexception at the beginning of the for loop </p> <p>the application is windows form application </p> <p>here is the code</p> <p>=========================================</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Drawing2D; namespace HistgramApp { public partial class Form1 : Form { Image img; Bitmap image; int temp; int r = 0, g = 0, b = 0; public Form1() { InitializeComponent(); this.Text = "HistogramApp"; this.AutoScroll = true; } //Generate new image dimensions public Size GenerateImageDimensions(int currW, int currH, int destW, int destH) { //double to hold the final multiplier to use when scaling the image double multiplier = 0; //string for holding layout string layout; //determine if it's Portrait or Landscape if (currH &gt; currW) layout = "portrait"; else layout = "landscape"; switch (layout.ToLower()) { case "portrait": //calculate multiplier on heights if (destH &gt; destW) { multiplier = (double)destW / (double)currW; } else { multiplier = (double)destH / (double)currH; } break; case "landscape": //calculate multiplier on widths if (destH &gt; destW) { multiplier = (double)destW / (double)currW; } else { multiplier = (double)destH / (double)currH; } break; } //return the new image dimensions return new Size((int)(currW * multiplier), (int)(currH * multiplier)); } //Resize the image private void SetImage(PictureBox pb) { try { //create a temp image Image img = pb.Image; //calculate the size of the image Size imgSize = GenerateImageDimensions(img.Width, img.Height, this.pictureBox1.Width, this.pictureBox1.Height); //create a new Bitmap with the proper dimensions Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height); //create a new Graphics object from the image Graphics gfx = Graphics.FromImage(img); //clean up the image (take care of any image loss from resizing) gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; //empty the PictureBox pb.Image = null; //center the new image pb.SizeMode = PictureBoxSizeMode.CenterImage; //set the new image pb.Image = finalImg; } catch (System.Exception e) { MessageBox.Show(e.Message); } } //Sample usage private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK ) { String sourcefile = ofd.FileName; String sourcefolder = Path.GetDirectoryName(ofd.FileName); // pictureBox1.ImageLocation = sourcefile; img = Image.FromFile(sourcefile); pictureBox1.Image = img; // pictureBox1.Height = img.Height; // pictureBox1.Width = img.Width; Size imgsize = GenerateImageDimensions(img.Width, img.Height, pictureBox1.Width, pictureBox1.Height); // Loop through the images pixels to reset color. } } private void Form1_Scroll(object sender, ScrollEventArgs e) { } private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void pictureBox1_SizeChanged(object sender, EventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } public void calc() { int i, j; i = 0; j = 0; // HERE IS THE EXCEPTION!! for ( i = 0; i &lt; 22; i++) { for ( j = 0; j &lt; 22; j++) { Color pixelColor = image.GetPixel(i, j); if (textBox1.Text == pixelColor.R.ToString()) { r++; } if (textBox1.Text == pixelColor.G.ToString()) { g++; } if (textBox1.Text == pixelColor.B.ToString()) { b++; } else ; } } label4.Text = r.ToString(); label5.Text = g.ToString(); label6.Text = b.ToString(); } private void button2_Click(object sender, EventArgs e) { calc(); } private void label6_Click(object sender, EventArgs e) { } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload