【问题标题】:Token Parsing Error .. encountered \' .. in Beanshell?令牌解析错误 .. 在 Beanshell 中遇到 \' ..?
【发布时间】:2012-06-27 23:10:07
【问题描述】:

我正在尝试运行以下 Beanshell 代码--

assignee.toString()!=''

这里的 'assignee' 是 Beanshell 上下文中的一个变量。

但我收到以下错误--

Caused by: Sourced file: inline evaluation of: ``assignee.toString()!='';'' Token Parsing Error: Lexical error at line 1, column 23.  Encountered: "\'" (39), after : "\'": <at unknown location>

at bsh.Interpreter.eval(Unknown Source)
at bsh.Interpreter.eval(Unknown Source)
at bsh.Interpreter.eval(Unknown Source)
at org.webharvest.runtime.scripting.BeanShellScriptEngine.eval(BeanShellScriptEngine.java:104)

我在这里做错了什么?我想检查变量 'assignee' 是否具有空值...完成此操作的正确方法是什么?

【问题讨论】:

    标签: java beanshell


    【解决方案1】:

    问题在于'c' 是字符文字,但''(“无字符”的字符文字?)Java 语法无效。 (即使它确实解析了String == char 也会出现类型错误。)

    也许是""(一个空字符串)文字?

    重要提示:即使这没有语法错误,通常也不应该将字符串与==(或!=)进行比较,因为结果可以出乎意料.. 关于为什么以及如何正确比较 Java 中的 String 对象有 很多 的问题:例如见String.equals(..)String.isEmpty()

    编码愉快!

    【讨论】:

      【解决方案2】:

      直接和null比较:

      if(assignee.toString() != null)
      {
         do.something();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2022-01-02
        • 1970-01-01
        • 2018-08-14
        • 2019-02-19
        • 2017-11-21
        相关资源
        最近更新 更多