简体   繁体   English

使用jruby时出现NullPointerException

[英]NullPointerException while using jruby

I embed jruby script engine into my java program by using javax.script.ScriptEngineManager 我通过使用javax.script.ScriptEngineManager将jruby脚本引擎嵌入到我的Java程序中

I made some jruby code that end with do ~ end block, after running all code, NullPointerException occured. 我做了结尾一些JRuby的代码do ~ end块,正在运行的所有代码之后,NullPointerException异常发生。 but code ends with any other statement, no exception occurs. 但是代码以任何其他语句结尾,不会发生任何异常。

version : 1.7.19 版本:1.7.19

Caused by: java.lang.NullPointerException
    at org.jruby.embed.variable.Argv.updateARGV(Argv.java:169)
    at org.jruby.embed.variable.Argv.retrieve(Argv.java:158)
    at org.jruby.embed.variable.VariableInterceptor.retrieve(VariableInterceptor.java:154)
    at org.jruby.embed.internal.BiVariableMap.retrieve(BiVariableMap.java:378)
    at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:124)

in ARGV.java updateARGV 在ARGV.java中updateARGV

if (vars.containsKey((Object)name)) {
    var = vars.getVariable((RubyObject)receiver.getRuntime().getTopSelf(), name);
        var.setRubyObject(argv);

vars.getVariable returned null because of isReceiverIdentical return false in BiVariableMap.java vars.getVariable返回null,因为BiVariableMap.java中的isReceiverIdentical返回false

if (var.isReceiverIdentical(receiver)) {
    return var;
}

In isReceiverIdentical, this method just compare receiver with BiVariable's receiver usgin '=='. 在isReceiverIdentical中,此方法仅将接收器与BiVariable的接收器usgin'=='进行比较。

Is this jruby bug? 这是jruby错误吗? Or do I have to do something for this? 还是我必须为此做些事情? If you need more information about this problem, plz comment it! 如果您需要有关此问题的更多信息,请注释!


I got ScriptEngine(engine) from ScriptEngineManager and set some java instance and method like this 我从ScriptEngineManager获得ScriptEngine(engine)并设置了一些Java实例和方法,如下所示

engine.put("this", console);
engine.eval("$command = $this.java_method :command, [java.lang.String]");

here is my test ruby code. 这是我的测试红宝石代码。 result and tab is java object that has some method return String and list. resulttab是具有某些方法返回String和list的java对象。

result = $command.call "something to pass"
puts result.getMessage
tabular = result.getData

tabular.each do |tab|
  rows = tab.getRows
  rows.each do |row|
    puts row
  end
  puts tab.getColumnNames
end

I had created ruby type object in my java code by creating new Ruby object... 我已经通过创建新的Ruby对象在Java代码中创建了ruby类型的对象...

This causes checking fail in updateARGV because a receiver that register variable in BiVariableMap and another receiver that update variable are different. 这会导致在updateARGV检查失败,因为在updateARGV中注册了变量的BiVariableMap与在更新变量中的另一个接收者是不同的。

So, I got a Ruby object from new ScriptingContainer (from it we can always get a same Ruby object if local context is singleton) and used it to create new ruby type object in my java code. 因此,我从新的ScriptingContainer获得了一个Ruby对象(如果本地上下文是单例,从中我们总是可以得到相同的Ruby对象),并使用它在我的Java代码中创建新的ruby类型的对象。

Reference: https://github.com/jruby/jruby/wiki/RedBridge#Singleton 参考: https : //github.com/jruby/jruby/wiki/RedBridge#Singleton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM