optional no value present

问题描述

 

No value present, app直接报错出来,直接找optional为什么会报这个错,

/**
 * If a value is present in this {@code Optional}, returns the value,
 * otherwise throws {@code NoSuchElementException}.
 *
 * @return the non-null value held by this {@code Optional}
 * @throws java.util.NoSuchElementException if there is no value present
 * @see Optional#isPresent()
 */
public T get() {
    if (value == null) {
        throw new NoSuchElementException("No value present");
    }
    return value;
}

 

找到 get使用的地方 ,查看get前面是不是空.

CustomerBaseInfo customerBaseInfo = 
    customerService.getCustomer(relavant.getCardId(), Integer.valueOf(relavant.getCardType())).get();

发现getCustomer获取到的optional是空.

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"No value present"是一个常见的异常消息,它表示在使用Optional类的get()方法时发生了异常。在get()方法中,如果Optional中没有值,就会抛出NoSuchElementException异常,并显示"No value present"的错误消息。这意味着在使用get()方法之前,我们应该使用isPresent()方法来检查Optional对象中是否有值。另外,我们还可以使用orElse()方法来避免异常,它会返回Optional对象中的值,如果值为空,则返回指定的替代值。所以,当遇到"No value present"的异常时,我们可以通过使用isPresent()方法或者orElse()方法来避免这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决java.util.NoSuchElementException: No value present 异常问题](https://blog.csdn.net/qq_39213969/article/details/105779112)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java8 新特性踩坑篇,java.util.NoSuchElementException: No value present](https://blog.csdn.net/weixin_45031612/article/details/125287048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值