NullReferenceException was unhandled - C#

Узнай цену своей работы

Формулировка задачи:

При отладке выскакивает такая вот кака.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MathNet.Numerics.LinearAlgebra;
  7. using MathNet.Numerics.LinearAlgebra.Double;
  8. namespace MultiplyMatrixNumerics
  9. {
  10. class Program
  11. {
  12. /* static void mult(Matrix<double> A, Matrix<double> B, ref Matrix<double> C)
  13. {
  14. C = A.ConjugateTransposeAndMultiply(B);
  15. }
  16. * */
  17. static int Clas = 6;
  18. static void Main(string[] args)
  19. {
  20. Matrix<double> AParcel = DenseMatrix.OfArray(new double[,] {
  21. {1,1,1,1},
  22. {1,2,3,4},
  23. {4,3,2,1}});
  24. Matrix<double> B = DenseMatrix.OfArray(new double[,] {
  25. {1,2,3,4},
  26. {5,6,7,8},
  27. {4,3,2,1}});
  28. Matrix<double> B_Tr = B.Transpose();
  29. Matrix<double> AB1 = AParcel.Multiply(B_Tr);
  30. //Console.WriteLine(C.ToString());
  31. // A = B.SubMatrix(1, 2, 0, 2);
  32. //Console.WriteLine(A.ToString());
  33. Matrix<double> AParcel2 = DenseMatrix.OfArray(new double[AParcel.RowCount, AParcel.ColumnCount]);
  34. for (int i = 0; i < AParcel.RowCount; i++)
  35. {
  36. for (int j = 0; j < AParcel.ColumnCount; j++)
  37. {
  38. AParcel2[i, j] = AParcel[i, j] * AParcel[i, j];
  39. }
  40. }
  41. Matrix<double> B_Tr2 = DenseMatrix.OfArray(new double[B_Tr.RowCount, B_Tr.ColumnCount]);
  42. for (int i = 0; i < B_Tr.RowCount; i++)
  43. {
  44. for (int j = 0; j < B_Tr.ColumnCount; j++)
  45. {
  46. B_Tr2[i, j] = B_Tr[i, j] * B_Tr[i, j];
  47. }
  48. }
  49. // Console.WriteLine(AParcel2);
  50. Vector<double> aParcel = AParcel2.RowSums();
  51. Vector<double> bParcel = B_Tr2.ColumnSums();
  52. // aParcel.ToRowMatrix();
  53. // bParcel.ToColumnMatrix();
  54.  
  55. Matrix<double> RR1 = DenseMatrix.OfArray(new double[AB1.RowCount, AB1.ColumnCount]);
  56. for (int i = 0; i < AParcel.RowCount; i++)
  57. {
  58. for (int j = 0; j < B_Tr.ColumnCount; j++)
  59. {
  60. RR1[i, j] = aParcel[i] + bParcel[j] - 2 * AB1[i, j];
  61. // AB1.MapIndexedInplace((i, j, x) => aParcel[i] + bParcel[j] - 2 * x);
  62. }
  63. }
  64. //Console.WriteLine(RR1);
  65. //var MinElRow = Vector<double>.Build.DenseOfEnumerable(matrix.EnumerateRows().Select(x => x.Min()));
  66. //var MinElCol = Vector<double>.Build.DenseOfEnumerable(matrix.EnumerateColumns().Select(x => x.Min()));
  67. Vector<double> MinRR1Row = Vector<double>.Build.DenseOfEnumerable(RR1.EnumerateRows().Select(x => x.Min()));
  68. Vector<double> MinRR1Column = Vector<double>.Build.DenseOfEnumerable(RR1.EnumerateColumns().Select(x => x.Min()));
  69. Matrix<double> MinRR1RowM = MinRR1Row.ToRowMatrix();
  70. Matrix<double> MinRR1ColumnM = MinRR1Column.ToColumnMatrix();
  71. Vector<double> MinRR1RowM_Summ = MinRR1RowM.RowSums();
  72. Vector<double> MinRR1ColumnM_Summ = MinRR1ColumnM.ColumnSums();
  73.  
  74. Vector<double> V = MinRR1RowM_Summ / AB1.ColumnCount;
  75. Vector<double> V1 = MinRR1ColumnM_Summ / AB1.RowCount;
  76.  
  77. //Console.WriteLine(MinRR1ColumnM);
  78. // Console.WriteLine(MinRR1RowM);
  79. // Console.WriteLine(MinRR1RowM_Summ);
  80. // Console.WriteLine(MinRR1ColumnM_Summ);
  81. Console.WriteLine(V);
  82. Console.WriteLine(V1);
  83. /*
  84. for (int j = 0; j < AB1.ColumnCount; j++)
  85. {
  86. for (int i = 0; i < AB1.RowCount; i++)
  87. {
  88. }
  89. }
  90.  
  91. for (int i = 0; i < AB1.ColumnCount; i++)
  92. {
  93. for (int j = 0; j < AB1.RowCount; j++)
  94. {
  95. }
  96. }
  97. */
  98. //Console.WriteLine(RR1);
  99. //Console.WriteLine();
  100. // Matrix<double> cRowSums_Matrix = C.RowSums();
  101. //Matrix<double> cColumnSums_Matrix = C.ColumnSums();
  102. Vector<double> VV = null;
  103. Vector<double> VV1 = null;
  104. Vector<double> probability = DenseVector.OfArray(new double[] {2.1, 4.2, 4.3, 5.1, 7.8});
  105. VV = V;
  106. VV1 = V1;
  107. double PMAX = -1;
  108. double PMAX1 = -1;
  109. int JMAX = 1;
  110. int JMAX1 = 1;
  111. kol.ProbabilityMax(Clas, VV, VV1, out probability, PMAX, PMAX1, JMAX, JMAX1);
  112. Console.WriteLine(probability);
  113. Console.WriteLine("P",PMAX);
  114. Console.WriteLine("P1",PMAX1);
  115. Console.WriteLine("J",JMAX);
  116. Console.WriteLine("J1",JMAX);
  117. string h;
  118. h = Console.ReadLine();
  119. }
  120. }
  121. class kol
  122. {
  123. public static void ProbabilityMax(int Clas, Vector<double> VV, Vector<double> VV1, out Vector<double> probability, double PMAX, double PMAX1, int JMAX, int JMAX1)
  124. {
  125. probability = null;
  126. Vector<double> SumPrJ = null;
  127. Vector<double> pr = DenseVector.OfArray(new double[1]);//Clas
  128. for (int i = 0; i < 1; i++)//1 - Clas - 1
  129. {
  130. pr[i] = (VV1[i]) + (VV[i]);
  131. }
  132. pr.MapInplace(x => 1 / x);
  133. for (int j = 0; j <1; j++)//Clas - 1
  134. {
  135. SumPrJ += pr[j];
  136. }
  137. for (int i = 0; i < 1; i++)//Clas - 1
  138. {
  139. probability = (pr[i]) / (SumPrJ);
  140. }
  141. for (int jj = 0; jj < 1; jj++)//Clas - 1
  142. {
  143. if (probability[jj] > PMAX)
  144. {
  145. JMAX = jj;
  146. PMAX = probability[jj];
  147. }
  148. }
  149. probability[JMAX] = -1.0;
  150. for (int jj = 0; jj < 1; jj++) //Clas - 1
  151. {
  152. if (probability[jj] > PMAX1)
  153. {
  154. JMAX1 = jj;
  155. PMAX1 = probability[jj];
  156. }
  157. }
  158. probability[JMAX] = PMAX;
  159. }
  160. }
  161. }
Ошибка в данном цикле:
Листинг программы
  1. for (int j = 0; j <1; j++)//Clas - 1
  2. {
  3. SumPrJ += pr[j];
  4. }
Additional information: Ссылка на объект не указывает на экземпляр объекта.

Решение задачи: «NullReferenceException was unhandled»

textual
Листинг программы
  1. for (int j = 0; j <1; j++)//Clas - 1
  2.  {
  3.  SumPrJ += pr[j];
  4. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут