小飞侠0001 2022-05-11 20:27 采纳率: 50%
浏览 34
已结题

如何解决在eclipse上运行.scala遇到的报错?

match may not be exhaustive. It would fail on the following input: None

img

  • 写回答

4条回答 默认 最新

  • hyh123a 全栈领域新星创作者 2022-05-11 20:48
    关注
    获得2.50元问题酬金

    原因
    模式匹配错误导致的scale的语法错误,
    跟eclipse无关

    解决方法
    以下面代码为例:

    Option(Session.get().getAttribute("player")) match {
      case None => {
        val player = new Player(user.getEmail, user.getNickname).createOrGet
        Session.get().setAttribute("player", player)
      }
    }
    

    报错同样为:

    Warning:(35, 11) match may not be exhaustive.
    It would fail on the following input: 
    

    这段代码错误的原因在于:
    模式匹配时,您应该考虑所有可能的情况或提供“fallback”(case _ => ...)。Option可以是Someor None,但您只匹配None大小写。

    如果Session.get().getAttribute("player")返回Some(player),您将得到一个MatchError(异常)。

    由于您的代码似乎没有返回任何内容,因此我将在没有的情况下重新编写它match,只需检查isEmpty.

    if(Option(Session.get().getAttribute("player")).isEmpty) {
        val player = new Player(user.getEmail, user.getNickname).createOrGet
        Session.get().setAttribute("player", player)
    }
    

    尽管这与检查并没有太大区别Session.get().getAttribute("player") == null。

    参考链接:


    如有问题及时沟通

    评论

报告相同问题?

问题事件

  • 系统已结题 5月19日
  • 修改了问题 5月11日
  • 创建了问题 5月11日

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求