Skip to content

obj.first caused Java::JavaUtil::NoSuchElementException on an empty Java21 ArrayList #8526

@jimtng

Description

@jimtng

Java21 introduced List.getFirst() and getLast(). So now this happens only in Java21 (jruby-9.4.9.0) because JRuby is calling the Java method. The same code runs fine and returns nil in Java17

irb(main):004:0> a = java.util.ArrayList.new
=> #<Java::JavaUtil::ArrayList: []>
irb(main):005:0> a
=> #<Java::JavaUtil::ArrayList: []>
irb(main):006:0> a.first
java.util.ArrayList.getFirst(java/util/ArrayList.java:439): Java::JavaUtil::NoSuchElementException
        from jdk.internal.reflect.DirectMethodHandleAccessor.invoke(jdk/internal/reflect/DirectMethodHandleAccessor.java:103)
        from java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:580)
        from org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(org/jruby/javasupport/JavaMethod.java:300)
        from org.jruby.javasupport.JavaMethod.invokeDirect(org/jruby/javasupport/JavaMethod.java:164)
        from RUBY.evaluate((irb):6)
        from org.jruby.RubyKernel.evalCommon(org/jruby/RubyKernel.java:1315)
        from org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1277)
        from org.jruby.RubyKernel$INVOKER$s$0$3$eval.call(org/jruby/RubyKernel$INVOKER$s$0$3$eval.gen)
        from org.jruby.RubyKernel.loop(org/jruby/RubyKernel.java:1725)
        from org.jruby.RubyKernel$INVOKER$s$0$0$loop.call(org/jruby/RubyKernel$INVOKER$s$0$0$loop.gen)
        from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1426)
        from org.jruby.RubyKernel$INVOKER$s$rbCatch.call(org/jruby/RubyKernel$INVOKER$s$rbCatch.gen)
        from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1426)
        from org.jruby.RubyKernel$INVOKER$s$rbCatch.call(org/jruby/RubyKernel$INVOKER$s$rbCatch.gen)
        from RUBY.<main>(/home/openhab/.rbenv/versions/jruby-9.4.9.0/lib/ruby/gems/shared/gems/irb-1.4.2/exe/irb:11)
        from org.jruby.Ruby.runInterpreter(org/jruby/Ruby.java:1258)
        ... 13 levels...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions