【问题标题】:NullReferenceException even though no object is nullNullReferenceException 即使没有对象为空
【发布时间】:2014-07-13 01:08:18
【问题描述】:

我正在使用决策树来决定图像中的像素是属于第 0 组还是第 1 组。训练图片为 1920 x 1080。上半部分是第 1 组像素,下半部分是第 0 组像素(每次除了 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);
}

我得到An unhandled exception of type 'System.NullReferenceException' occurred in Accord.MachineLearning.dll 标记id3learning.Run(inputs, outputs);,但id3learninginputsoutputs 都不是null

这是异常消息:

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: 

这里是 checkArgs:http://dotnetinside.com/pt/type/Accord.MachineLearning/ID3Learning/2.12.0.0

这种行为的原因是什么?

【问题讨论】:

标签: c# machine-learning decision-tree accord.net


【解决方案1】:

即使异常没有说明异常发生在checkArgs 的哪一行,也有可能发生在这一行:

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

你说inputs 本身不为空。它的内容(内部数组)呢?它们是空的吗?

如果第一个内部数组为空,inputs[0].Length 会抛出异常。

【讨论】:

    猜你喜欢
    • 2013-03-04
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多