简体   繁体   English

用于将 null 转换为类的 JRuby 等效项

[英]JRuby equivalent for casting null to class

I try to use the Java weka machine learning lib through JRuby.我尝试通过 JRuby 使用 Java weka 机器学习库。 It works fine so far, there is only one thing I'm wondering:到目前为止它运行良好,我想知道的只有一件事:

In order to create a string Attribute, you can use the same contructor as for nominal Attributes in Java, but with the second parameter being a null casted to FastVector:为了创建字符串属性,您可以使用与 Java 中的名义属性相同的构造函数,但第二个参数是一个空值转换为 FastVector:

Attribute attribute = new Attribute("name", (FastVector) null);

Also see this Stackoverflow post and the weka doc for Attribute .另请参阅此 Stackoverflow 帖子Attributeweka 文档

In JRuby, if you try to pass just nil , eg:在 JRuby 中,如果您尝试只传递nil ,例如:

java_import 'weka.core.Attribute'
attribute = Attribute.new('name', nil)

it will raise a Java::JavaLang::NullPointerException error.它将引发Java::JavaLang::NullPointerException错误。

The full stack trace is:完整的堆栈跟踪是:

Java::JavaLang::NullPointerException: 
from weka.core.Attribute.<init>(weka/core/Attribute.java:303)
from weka.core.Attribute.<init>(weka/core/Attribute.java:290)
from java.lang.reflect.Constructor.newInstance(java/lang/reflect/Constructor.java:423)
from RUBY.<eval>((irb):7)
from org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:978)
from org.jruby.RubyKernel.loop(org/jruby/RubyKernel.java:1291)
from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1098)
from org.jruby.RubyKernel.catch(org/jruby/RubyKernel.java:1098)
from Users.pgoetze.$_dot_rvm.rubies.jruby_minus_9_dot_0_dot_1_dot_0.bin.irb.<top>(/Users/pgoetze/.rvm/rubies/jruby-9.0.1.0/bin/irb:13)
from java.lang.invoke.MethodHandle.invokeWithArguments(java/lang/invoke/MethodHandle.java:627)

Is there a way to create a null vector in JRuby, that can be passed as the second argument instead of the nil ?有没有办法在 JRuby 中创建一个空向量,它可以作为第二个参数而不是nil传递?

If not, what might be the way to create a string Attribute?如果没有,创建字符串属性的方法可能是什么?

As JRuby wiki states you are supposed to use reflection to obtain a reference to a constructor specifying types of its arguments.正如JRuby wiki 所述,您应该使用反射来获取对指定其参数类型的构造函数的引用。

java_import java.util.List
java_import weka.core.Attribute
#...
constructor = Attribute.java_class.declared_constructor(java.lang.String, java.util.List)
attribute = constructor.new_instance('name', nil)

Notes:笔记:

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

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