即使没有对象为空也出现NullReferenceException

我使用决策树来决定图像中的一个像素是属于第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) //图片的一半是第1组,另一半是第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

这种行为的原因是什么?


回答:

尽管异常信息没有说明checkArgs中的哪一行发生了异常,但有可能是在这一行发生的:

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

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

如果第一个内部数组是null,那么inputs[0].Length将会抛出异常。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注