永远不会将字段xxx分配给,并始终将其默认值设置为null

时间:2011-01-26 23:05:07

标签: c# oop debugging

任何人都知道这个问题是什么?

我收到了此警告永远不会将字段xxx分配给private static Quantizer quantit;

上的默认值

我不知道该怎么做才能修复,因为当我尝试使用quantit.Quantize()时调试说:“对象引用未设置为对象的实例。”并指向{ {1}}

代码:

au = quantit.Quantize();

班级:

public class Quantization : System.Windows.Forms.Form
{ 
    private static Quantizer quantit;

    private Button btnLoad;
    private PictureBox imgPhoto;

    public Quantization()
    {

        btnLoad = new Button();
        btnLoad.Text = "&Load";
        btnLoad.Left = 10;
        btnLoad.Top = 10;
        btnLoad.Click += new System.EventHandler(this.OnLoadClick);

        imgPhoto = new PictureBox();
        imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        imgPhoto.Width = this.Width / 2;
        imgPhoto.Height = this.Height / 2;
        imgPhoto.Left = (this.Width - imgPhoto.Width) / 2;
        imgPhoto.Top = (this.Height - imgPhoto.Height) / 2;
        imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;

        this.Controls.Add(btnLoad);
        this.Controls.Add(imgPhoto);  
    }

    protected void OnLoadClick(object sender, System.EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();

      dlg.Title = "Open Image";
      dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;

      if (dlg.ShowDialog() == DialogResult.OK)
      {
          Bitmap au;

          //Image bmp = Image.FromFile("D:\\Documents and Settings\\kiosk.suprisul\\My Documents\\foto1.jpg");

          au = quantit.Quantize();
          imgPhoto.Image = au;
          //imgPhoto.Image = bmp;
          //imgPhoto.Image = au;
          //new Bitmap(dlg.OpenFile());
      }

      dlg.Dispose();
    }
    [STAThread]
    static void Main(string[] args)
    {

        //Image bmp;

        //bmp = Image.FromFile("teste.jpg");
        //PaintEventArgs e;
        //teste2.Quantize(bmp);


        Application.Run(new Quantization());

        /*
        System.Console.WriteLine("Hello, World!");
        System.Console.ReadLine();*/

    }
}

4 个答案:

答案 0 :(得分:8)

编译器警告您quantit永远不会被初始化,并始终为null

您可能应该使用派生自ImageManipulation.Quantizer的类的实例对其进行初始化(您无法实例化Quantizer本身,因为它是abstract类:

private static Quantizer quantit = new QuantizerImplementation();

答案 1 :(得分:3)

您永远不会将Quantizer类的实例分配给quantit静态变量,因此它将保留为null引用,并在您尝试使用时生成该异常其中一种方法。要解决此问题,请在使用之前使用new Quantizer对象初始化该成员。

顺便说一句,我不确定你希望那个变量是static

修改

我刚才看到Quantizer是一个抽象类...然后你不能直接实例化它,你有第一个来从它实现{{ 1}}方法(即abstractQuantizePixel)或使用另一个派生自GetPalette的现成类,然后使用此类的新实例初始化Quantizer字段

答案 2 :(得分:2)

通过类型名称访问静态成员,即

Quantization.quantit = {some value};

当然,因为它是私人的,你必须从里面这个类型,在这种情况下你可以使用:

quantit = {some value};

但是,我还会质疑静态是否适合这里,特别是如果您正在进行任何线程(或Web代码)。静态经常被过度使用(并且使用不当)。

答案 3 :(得分:2)

静态字段只是一个字段,其值由每个人共享。将其视为全局变量。问题是你仍然需要至少实例化一次。通常,这是在与声明相同的时间/地点完成的。

public static Quantizer quantit = new Quantizer(?);

我不太了解你想做什么,但我认为你真的不想在这里使用静态字段。我的猜测是你想根据一些输入参数(singlePass或doublePass)实例化/创建一个新的Quantizer实例。如果Quantizer类没有状态,则应将其设为单例。如果您想这样做,我建议您查看依赖注入容器,例如Castle Windsor,它可以更轻松地为您处理。