[英]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 帖子和Attribute的weka 文档。
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:笔记:
weka.core.FastVector
is just an extension of ArrayList
weka.core.FastVector
只是ArrayList
的扩展weka.core.Attribute
contains a constructor with single String
argument which maybe does what you are trying to achieve weka.core.Attribute
包含一个带有单个String
参数的构造函数,它可能完成您想要实现的目标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.