处理数据库中的空值

时间:2019-04-01 11:24:58

标签: java

我使用此代码通过ID获取商家名称。

@GetMapping("pages")
public Page<WpfPaymentsDTO> pages(@RequestParam(value = "page") int page, @RequestParam(value = "size") int size) {
    return wpfPaymentsService.findAll(page, size)
                             .map(g -> WpfPaymentsDTO.builder()
                             .id(g.getId())
                             .status(g.getStatus())
                             .merchant_id(getMerchantName(g.getMerchant_id()))
                             .build());  
}

private String getMerchantName(Integer id) {      
    return Optional.ofNullable(id)
                   .flatMap(i -> merchantService.findById(i))
                   .map(Merchants::getName)
                   .orElse("");
}

但是当找不到名称时,我得到java.lang.NullPointerException: null at this line: .merchant_id(getMerchantName(g.getMerchant_id())),因为值g.getMerchant_id()null进入数据库。

对此有解决方案吗?

Stacktrace(第一行)

14:48:55.849 [default task-1] ERROR o.s.b.w.s.support.ErrorPageFilter - Forwarding to error page from request [/wpf_payments/pages] due to exception[null]
java.lang.NullPointerException: null
    at deployment.datalis_admin.war//org.datalis.admin.backend.restapi.WpfPaymentsController.lambda$pages$1(WpfPaymentsController.java:169)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:927)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    at deployment.datalis_admin.war//org.springframework.data.domain.Chunk.getConvertedContent(Chunk.java:167)
    at deployment.datalis_admin.war//org.springframework.data.domain.PageImpl.map(PageImpl.java:106)
    at deployment.datalis_admin.war//org.datalis.admin.backend.restapi.WpfPaymentsController.pages(WpfPaymentsController.java:164)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)

2 个答案:

答案 0 :(得分:4)

如果NullPointerException出现在该行,我怀疑它与merchand_id有关。您可以确保.status(g.getStatus())不会返回null吗?

仅当“ g”为null时,Java才会在您提到的行报告NullPointerException。但这不可能,因为以前已经使用过。

答案 1 :(得分:0)

可能有几种解决方案。根据您提供的代码,最简单的方法是过滤掉所有g.getMerchant_id()null的商户:

wpfPaymentsService.findAll(page, size)
                  .filter(g -> g.getMerchant_id() != null)
                  .map(g -> WpfPaymentsDTO.builder()
                                          .id(g.getId())
                                          .status(g.getStatus())
                                          .merchant_id(getMerchantName(g.getMerchant_id()))
                                          .build()
                  );

这应该可以解决NullPointerException,但是可能不需要。这取决于您的要求。结果中是否允许没有ID的商家?