onError java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'double java.lang.Double.doubleValue()'



我已经编写了以下代码,以查找位于某些预算的漫画的 pagecount 。。

首先,我试图提出具有这样的体系结构的代码:

  • 流给出了 marvelcomic 对象的价格。
  • 我将流从流中的 marvelcomic 对象的价格总结为以前的漫画的价格,这些漫画的价格下降了,并检查它是否是<预算
  • 如果是的,则我将 pagecount marvelcomic 对象的对象带有以前的 marvelcomic 对象的pagecount和从流下来的对象。
  • 如果是,则称为订阅者的 onnext 被称为。

由于我无法像上述步骤中提到的那样编写代码的方法,因此我求助于用反应性编程捣碎命令编程。结果,我编写了以下代码:

Observable.fromIterable(getMarvelComicsList()).
                map(new Function<MarvelComic, HashMap<String, Double>>() {
                    @Override
                    public HashMap<String, Double> apply(@NonNull MarvelComic marvelComic) throws Exception {
                        HashMap<String, Double> map = new HashMap<String, Double>();
                        map.put("price", Double.valueOf(marvelComic.getPrice()));
                        map.put("pageCount", Double.valueOf(marvelComic.getPageCount()));
                        map.put("comicCount", Double.valueOf(marvelComic.getPageCount()));
                        return map;
                    }
                })
                .scan(new HashMap<String, Double>(), new BiFunction<HashMap<String, Double>, HashMap<String, Double>, HashMap<String, Double>>() {
                    @Override
                    public HashMap<String, Double> apply(@NonNull HashMap<String, Double> inputMap, @NonNull HashMap<String, Double> newValueMap) throws Exception {
                        double sum = inputMap.get("price")+newValueMap.get("price");
                        double count = inputMap.get("pageCount")+newValueMap.get("pageCount");
                        double comicCount = inputMap.get("comicCount")+newValueMap.get("comicCount");
                        HashMap<String, Double> map = new HashMap<String, Double>();
                        map.put("price", sum);
                        map.put("pageCount", count);
                        map.put("comicCount", comicCount);
                        return map;
                    }
                })
                .takeWhile(new Predicate<HashMap<String, Double>>() {
                    @Override
                    public boolean test(@NonNull HashMap<String, Double> stringDoubleHashMap) throws Exception {
                        return stringDoubleHashMap.get("price") < budget;
                    }
                })
                .subscribe(new DisposableObserver<HashMap<String, Double>>() {
                    @Override
                    public void onNext(HashMap<String, Double> stringDoubleHashMap) {
                        double sum = stringDoubleHashMap.get("price");
                        double pageCount = stringDoubleHashMap.get("pageCount");
                        double comicCount = stringDoubleHashMap.get("comicCount");
                        Timber.e("sum %s  pageCount %s  ComicCount: %s", sum, pageCount, comicCount);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Timber.e("onError %s", e.fillInStackTrace());
                    }
                    @Override
                    public void onComplete() {
                        Timber.e("onComplete");
                    }
                });

我的保留:

  1. 每次在map(), scan()中创建一个新的哈希图?
  2. 如何进一步改进此代码?

问题:

此代码在onError中给出 nullpointerexception ,因为map.get("price")scan()中返回null。我不太确定原因。

错误:

 onError java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

注意:

Hashmap不是零,由于某种原因,双字段被返回为空。我试图弄清楚如何。

问题可能是您由于

而拥有一个空的初始地图
.scan(new HashMap<String, Double>(), ...)

第一个真实地图从上游到达时,您正在尝试从该空的初始映射中获取值:

double sum = inputMap.get("price")+newValueMap.get("price");

我假设您想通过使用scan进行属性的运行汇总,因此您应该尝试将第一个上游值排放到IS的scan(BiFunction),然后开始将上一个上游值与新的上游值组合。

另外,您可以使用默认值进行初始化的new HashMap<>(),并避免使用NPE:

HashMap<String, Double> initialMap = new HashMap<String, Double>();
initialMap.put("price", 0.0d);
initialMap.put("pageCount", 0.0d);
initialMap.put("comicCount", 0.0d);
Observable.fromIterable(getMarvelComicsList()).
            map(new Function<MarvelComic, HashMap<String, Double>>() {
                @Override
                public HashMap<String, Double> apply(@NonNull MarvelComic marvelComic) {
                    HashMap<String, Double> map = new HashMap<String, Double>();
                    map.put("price", Double.valueOf(marvelComic.getPrice()));
                    map.put("pageCount", Double.valueOf(marvelComic.getPageCount()));
                    map.put("comicCount", Double.valueOf(marvelComic.getPageCount()));
                    return map;
                }
            })
            .scan(initialMap, 
            new BiFunction<HashMap<String, Double>, 
                    HashMap<String, Double>, HashMap<String, Double>>() {
                @Override
                public HashMap<String, Double> apply(
                         @NonNull HashMap<String, Double> inputMap, 
                         @NonNull HashMap<String, Double> newValueMap) {
                    double sum = inputMap.get("price")+newValueMap.get("price");
                    double count = inputMap.get("pageCount")
                        +newValueMap.get("pageCount");
                    double comicCount = inputMap.get("comicCount")
                        +newValueMap.get("comicCount");
                    HashMap<String, Double> map = new HashMap<String, Double>();
                    map.put("price", sum);
                    map.put("pageCount", count);
                    map.put("comicCount", comicCount);
                    return map;
                }
            })
            // etc.

我试图用不同的方法来解决您的问题,这不会抛出任何NPE。

请不要将hashmaps用作数据架构。这是内在发生的事情。您应该创建有意义的类。

此外,订户不应执行任何业务词法。订户实际上应该只使用结果并做副作用,例如更改视图。

我希望我确实正确理解了您的问题。

@Test
void name() {
    ArrayList<MarvelComic> marvelComics = Lists.newArrayList(new MarvelComic(10, 200), new MarvelComic(3, 133), new MarvelComic(5, 555), new MarvelComic(32, 392));
    final double BUDGET = 20.0;
    Observable<Result> resultObservable = Observable.fromIterable(marvelComics)
            .scan(Result.IDENTITY, (result, marvelComic) -> {
                double priceSum = result.sumPrice + marvelComic.getPrice();
                if (priceSum <= BUDGET) {
                    int pageCount = result.sumPageCount + marvelComic.getPageCount();
                    int comicCount = result.comicCount + 1;
                    return new Result(pageCount, priceSum, comicCount);
                }
                return Result.IDENTITY;
            })
            .skip(1) // because first Value would be Result.IDENTITY
            .takeWhile(result -> result != Result.IDENTITY);
    TestObserver<Result> test = resultObservable.test().assertValueCount(3);
    Result result1 = test.values()
            .stream()
            .reduce((result, result2) -> result2)
            .get();
    assertThat(result1.comicCount).isEqualTo(3);
    assertThat(result1.sumPageCount).isEqualTo(888);
    assertThat(result1.sumPrice).isEqualTo(18);
}
class MarvelComic {
    private final double price;
    private final int pageCount;
    MarvelComic(double price, int pageCount) {
        this.price = price;
        this.pageCount = pageCount;
    }
    public double getPrice() {
        return price;
    }
    public int getPageCount() {
        return pageCount;
    }
}
static class Result {
    private final int sumPageCount;
    private final double sumPrice;
    private final int comicCount;
    Result(int sumPageCount, double sumPrice, int comicCount) {
        this.sumPageCount = sumPageCount;
        this.sumPrice = sumPrice;
        this.comicCount = comicCount;
    }
    static Result IDENTITY = new Result(0, 0, 0);
}

我认为您可以获得getPrice(),getPageCount()方法的空值或空白值

 map.put("price", Double.valueOf(marvelComic.getPrice()));
                            map.put("pageCount", Double.valueOf(marvelComic.getPageCount()));
                            map.put("comicCount", Double.valueOf(marvelComic.getPageCount()));

或您可以使用Double.parseDouble();方法

您已经使用了DoubleValue((函数3次,

map.put("price", Double.valueOf(marvelComic.getPrice()));
map.put("pageCount", Double.valueOf(marvelComic.getPageCount()));
map.put("comicCount", Double.valueOf(marvelComic.getPageCount()));

确认 marvelcomic 具有价格和页面count 的值而且我认为您正在添加 pageCount AS comiccount in 映射

我建议尝试尝试捕获和打印错误以了解根本原因

最新更新