NullReferenceException was unhandled - C#
Формулировка задачи:
При отладке выскакивает такая вот кака.
Ошибка в данном цикле:
Additional information: Ссылка на объект не указывает на экземпляр объекта.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra.Double; namespace MultiplyMatrixNumerics { class Program { /* static void mult(Matrix<double> A, Matrix<double> B, ref Matrix<double> C) { C = A.ConjugateTransposeAndMultiply(B); } * */ static int Clas = 6; static void Main(string[] args) { Matrix<double> AParcel = DenseMatrix.OfArray(new double[,] { {1,1,1,1}, {1,2,3,4}, {4,3,2,1}}); Matrix<double> B = DenseMatrix.OfArray(new double[,] { {1,2,3,4}, {5,6,7,8}, {4,3,2,1}}); Matrix<double> B_Tr = B.Transpose(); Matrix<double> AB1 = AParcel.Multiply(B_Tr); //Console.WriteLine(C.ToString()); // A = B.SubMatrix(1, 2, 0, 2); //Console.WriteLine(A.ToString()); Matrix<double> AParcel2 = DenseMatrix.OfArray(new double[AParcel.RowCount, AParcel.ColumnCount]); for (int i = 0; i < AParcel.RowCount; i++) { for (int j = 0; j < AParcel.ColumnCount; j++) { AParcel2[i, j] = AParcel[i, j] * AParcel[i, j]; } } Matrix<double> B_Tr2 = DenseMatrix.OfArray(new double[B_Tr.RowCount, B_Tr.ColumnCount]); for (int i = 0; i < B_Tr.RowCount; i++) { for (int j = 0; j < B_Tr.ColumnCount; j++) { B_Tr2[i, j] = B_Tr[i, j] * B_Tr[i, j]; } } // Console.WriteLine(AParcel2); Vector<double> aParcel = AParcel2.RowSums(); Vector<double> bParcel = B_Tr2.ColumnSums(); // aParcel.ToRowMatrix(); // bParcel.ToColumnMatrix(); Matrix<double> RR1 = DenseMatrix.OfArray(new double[AB1.RowCount, AB1.ColumnCount]); for (int i = 0; i < AParcel.RowCount; i++) { for (int j = 0; j < B_Tr.ColumnCount; j++) { RR1[i, j] = aParcel[i] + bParcel[j] - 2 * AB1[i, j]; // AB1.MapIndexedInplace((i, j, x) => aParcel[i] + bParcel[j] - 2 * x); } } //Console.WriteLine(RR1); //var MinElRow = Vector<double>.Build.DenseOfEnumerable(matrix.EnumerateRows().Select(x => x.Min())); //var MinElCol = Vector<double>.Build.DenseOfEnumerable(matrix.EnumerateColumns().Select(x => x.Min())); Vector<double> MinRR1Row = Vector<double>.Build.DenseOfEnumerable(RR1.EnumerateRows().Select(x => x.Min())); Vector<double> MinRR1Column = Vector<double>.Build.DenseOfEnumerable(RR1.EnumerateColumns().Select(x => x.Min())); Matrix<double> MinRR1RowM = MinRR1Row.ToRowMatrix(); Matrix<double> MinRR1ColumnM = MinRR1Column.ToColumnMatrix(); Vector<double> MinRR1RowM_Summ = MinRR1RowM.RowSums(); Vector<double> MinRR1ColumnM_Summ = MinRR1ColumnM.ColumnSums(); Vector<double> V = MinRR1RowM_Summ / AB1.ColumnCount; Vector<double> V1 = MinRR1ColumnM_Summ / AB1.RowCount; //Console.WriteLine(MinRR1ColumnM); // Console.WriteLine(MinRR1RowM); // Console.WriteLine(MinRR1RowM_Summ); // Console.WriteLine(MinRR1ColumnM_Summ); Console.WriteLine(V); Console.WriteLine(V1); /* for (int j = 0; j < AB1.ColumnCount; j++) { for (int i = 0; i < AB1.RowCount; i++) { } } for (int i = 0; i < AB1.ColumnCount; i++) { for (int j = 0; j < AB1.RowCount; j++) { } } */ //Console.WriteLine(RR1); //Console.WriteLine(); // Matrix<double> cRowSums_Matrix = C.RowSums(); //Matrix<double> cColumnSums_Matrix = C.ColumnSums(); Vector<double> VV = null; Vector<double> VV1 = null; Vector<double> probability = DenseVector.OfArray(new double[] {2.1, 4.2, 4.3, 5.1, 7.8}); VV = V; VV1 = V1; double PMAX = -1; double PMAX1 = -1; int JMAX = 1; int JMAX1 = 1; kol.ProbabilityMax(Clas, VV, VV1, out probability, PMAX, PMAX1, JMAX, JMAX1); Console.WriteLine(probability); Console.WriteLine("P",PMAX); Console.WriteLine("P1",PMAX1); Console.WriteLine("J",JMAX); Console.WriteLine("J1",JMAX); string h; h = Console.ReadLine(); } } class kol { public static void ProbabilityMax(int Clas, Vector<double> VV, Vector<double> VV1, out Vector<double> probability, double PMAX, double PMAX1, int JMAX, int JMAX1) { probability = null; Vector<double> SumPrJ = null; Vector<double> pr = DenseVector.OfArray(new double[1]);//Clas for (int i = 0; i < 1; i++)//1 - Clas - 1 { pr[i] = (VV1[i]) + (VV[i]); } pr.MapInplace(x => 1 / x); for (int j = 0; j <1; j++)//Clas - 1 { SumPrJ += pr[j]; } for (int i = 0; i < 1; i++)//Clas - 1 { probability = (pr[i]) / (SumPrJ); } for (int jj = 0; jj < 1; jj++)//Clas - 1 { if (probability[jj] > PMAX) { JMAX = jj; PMAX = probability[jj]; } } probability[JMAX] = -1.0; for (int jj = 0; jj < 1; jj++) //Clas - 1 { if (probability[jj] > PMAX1) { JMAX1 = jj; PMAX1 = probability[jj]; } } probability[JMAX] = PMAX; } } }
for (int j = 0; j <1; j++)//Clas - 1 { SumPrJ += pr[j]; }
Решение задачи: «NullReferenceException was unhandled»
textual
Листинг программы
for (int j = 0; j <1; j++)//Clas - 1 { SumPrJ += pr[j]; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д