简体   繁体   中英

NullReferenceException even though no object is null

I am using a decision tree to decide whether a pixel in an image belongs to group 0 or to group 1. The training picture is 1920 x 1080. The upper half are group 1 pixels, the lower half are group 0 pixels(each time except 255,255,255).

if (oFDBildDatei.ShowDialog() == DialogResult.OK)
{
    string path = oFDBildDatei.FileName;
    pictureBox1.Image = System.Drawing.Image.FromFile(path);

    int[][] inputs = new int[2073600][];    // 1920 x 1080 picture
    int[] outputs = new int[2073600];

    Bitmap bitmap = (Bitmap)pictureBox1.Image;
    int i = 0;
    for (int line = 0; line <= pictureBox1.Height; line++)
    {
        for (int column = 0; column <= pictureBox1.Width; column++)
        {
            Color ThreeColorValues = bitmap.GetPixel(column, line);
            if (ThreeColorValues.R == 255 && ThreeColorValues.G == 255 && ThreeColorValues.B == 255)
                continue;
            inputs[i] = new int[3];
            inputs[i][0] = (int)ThreeColorValues.R;
            inputs[i][1] = (int)ThreeColorValues.G;
            inputs[i][2] = (int)ThreeColorValues.B;
            if (line > pictureBox1.Height / 2) //Half of the picture is group 1, the other half is group 0
                outputs[i] = 1;
            else
                outputs[i] = 0;
            i++;
        }
    }


    DecisionVariable[] attributes =
    {
        new DecisionVariable("R",256),
        new DecisionVariable("G",256),
        new DecisionVariable("B",256)
    };
    int classCount = 2;

    baum = new DecisionTree(attributes, classCount);
    ID3Learning id3learning = new ID3Learning(baum);

    id3learning.Run(inputs, outputs);
}

I get An unhandled exception of type 'System.NullReferenceException' occurred in Accord.MachineLearning.dll marking id3learning.Run(inputs, outputs); but neither id3learning , inputs nor outputs is null .

This is the exception message:

System.NullReferenceException was unhandled
  _HResult=-2147467261
  _message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
  HResult=-2147467261
  IsTransient=false
  Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
  Source=Accord.MachineLearning
  StackTrace:
       bei Accord.MachineLearning.DecisionTrees.Learning.ID3Learning.checkArgs(Int32[][] inputs, Int32[] outputs)
       bei Accord.MachineLearning.DecisionTrees.Learning.ID3Learning.Run(Int32[][] inputs, Int32[] outputs)
       bei program.Form1.button1_Click(Object sender, EventArgs e) in e:\c#\Form1.cs:Zeile 125.
       ....
  InnerException: 

Here is checkArgs: http://dotnetinside.com/pt/type/Accord.MachineLearning/ID3Learning/2.12.0.0

What is the reason for this behaviour?

Even though the exception doesn't say in which line of checkArgs the exception occurred, it's possible it may have happened on this line:

if (inputs[i].Length != this.tree.InputCount)

You said inputs itself is not null. What about its contents (the inner arrays )? Are they null?

If the first inner array is null, inputs[0].Length will throw an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM