From 49f6c589c31a77bc610e5d8020f0cd8e8b821a94 Mon Sep 17 00:00:00 2001 From: Andreas Buchen Date: Sun, 10 May 2020 08:08:45 +0200 Subject: [PATCH] Fixed NPE when updating security prices and no latest security price exists Issue: #1492 --- .../abuchen/portfolio/model/Security.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/name.abuchen.portfolio/src/name/abuchen/portfolio/model/Security.java b/name.abuchen.portfolio/src/name/abuchen/portfolio/model/Security.java index eda62e0c36..1089b08920 100644 --- a/name.abuchen.portfolio/src/name/abuchen/portfolio/model/Security.java +++ b/name.abuchen.portfolio/src/name/abuchen/portfolio/model/Security.java @@ -334,12 +334,13 @@ public boolean addPrice(SecurityPrice price) { return addPrice(price, true); } - + /** * Adds security price to historical quotes. * - * @param overwriteExisting is used to decide on whether to keep or overwrite existing prices - * + * @param overwriteExisting + * is used to decide on whether to keep or overwrite existing + * prices * @return true if the historical quote was updated. */ public boolean addPrice(SecurityPrice price, boolean overwriteExisting) @@ -356,11 +357,10 @@ public boolean addPrice(SecurityPrice price, boolean overwriteExisting) else { SecurityPrice replaced = prices.get(index); - + // different prices are replaced only, if the source is manual, csv - // or html import, the value is 0.0 or it is the last given price - if (!replaced.equals(price) && (overwriteExisting || replaced.getValue() == 0.0 - || replaced.getDate().equals(latest.getDate()))) + // or html import, the value is 0.0 + if (!replaced.equals(price) && (overwriteExisting || replaced.getValue() == 0.0)) { // only replace if necessary -> UI might keep reference! prices.set(index, price); @@ -372,20 +372,31 @@ public boolean addPrice(SecurityPrice price, boolean overwriteExisting) } } } - + + /** + * Adds all prices to the list of prices unless a security price for that + * date already exists. However, the last historical date is overwritten as + * some quote provider include the latest security price in the list of + * historical prices. + */ public boolean addAllPrices(List prices) { if (prices.isEmpty()) return false; - + LocalDate now = LocalDate.now(); - + + LocalDate last = null; + if (!prices.isEmpty()) + last = prices.get(prices.size() - 1).getDate(); + boolean isUpdated = false; for (SecurityPrice p : prices) { if (p.getDate().isBefore(now)) { - boolean isAdded = addPrice(p, false); + boolean doOverwrite = p.getDate().equals(last); + boolean isAdded = addPrice(p, doOverwrite); isUpdated = isUpdated || isAdded; } }