From c0c656c82c19e31d8df7c152eb64e388359185b3 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 23 May 2023 07:33:49 +0300 Subject: [PATCH] Fix null reference --- RhSolutions.AddIn/Services/GuessReader.cs | 38 ++++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/RhSolutions.AddIn/Services/GuessReader.cs b/RhSolutions.AddIn/Services/GuessReader.cs index 9cb4bde..6a2acb6 100644 --- a/RhSolutions.AddIn/Services/GuessReader.cs +++ b/RhSolutions.AddIn/Services/GuessReader.cs @@ -55,9 +55,17 @@ public class GuessReader : IReader private bool IsProductColumn(Range column) { int successCounter = 0; + object[,] cells = column.Value2; + for (int row = 1; row < column.Rows.Count + 1; row++) { - if (ProductSku.TryParse(column.Cells[row, 1].Value2.ToString(), out IEnumerable skus)) + object currentCell = cells[row, 1]; + if (currentCell == null) + { + continue; + } + + if (ProductSku.TryParse(currentCell.ToString(), out IEnumerable skus)) { successCounter++; } @@ -84,6 +92,7 @@ public class GuessReader : IReader { continue; } + double value = 0.0; if (currentCell.GetType() == typeof(double)) @@ -115,26 +124,33 @@ public class GuessReader : IReader for (int row = 1; row < productColumn.Rows.Count + 1; row++) { - object[,] cells = amountColumn.Value2; - object currentCell = cells[row, 1]; + object[,] amountCells = amountColumn.Value2; + object currentAmountCell = amountCells[row, 1]; + + object[,] productCells = productColumn.Value2; + object currentProductCell = productCells[row, 1]; + double amountValue = 0.0; - if (ProductSku.TryParse(productColumn.Cells[row, 1].Value2.ToString(), out IEnumerable skus) && - currentCell != null) + + if (currentAmountCell == null || currentProductCell == null) + { + continue; + } + + if (ProductSku.TryParse(currentProductCell.ToString(), out IEnumerable skus)) { Product p = new(skus.First()) { Name = "Распознанный артикул" }; - - if (currentCell.GetType() == typeof(double)) + if (currentAmountCell.GetType() == typeof(double)) { - amountValue = (double)currentCell; + amountValue = (double)currentAmountCell; } - else if (currentCell.GetType() == typeof(string)) + else if (currentAmountCell.GetType() == typeof(string)) { - double.TryParse((string)currentCell, out amountValue); + double.TryParse((string)currentAmountCell, out amountValue); } - if (result.ContainsKey(p)) { result[p] += amountValue;