简体   繁体   中英

Ljava.lang.Object error - Matlab/Java

I am pretty new to java and I am trying to do some java involving nested classes and I am running it in Matlab . So I have my innerclass and my outerclass and I am trying to create an instance of this new class which takes two java.lang.Objects as it's constructor but when I try to do this I get an error and it says I am trying to pass through this Ljava.lang.Object as opposed to a java.lang.Object. Researched a little about this about how it's the encoded name but I don't really understand. Any help would be appreciated, my code (from Matlab ) is below!

>> p = innerclass.getConstructors();

>> p(1) 

ans = 

public innerclassName(java.lang.Object, java.lang.Object)

>> k=javaArray('java.lang.String',3);
>> k(1)=java.lang.String('a');
>> k(2)=java.lang.String('b');
>> k(3)=java.lang.String('c');

>> v=javaArray('java.lang.Integer',3,2);
>> v(1,1) = java.lang.Integer(1);
>> v(2,1) = java.lang.Integer(2);
>> v(3,1) = java.lang.Integer(3);
>> v(1,2) = java.lang.Integer(4);
>> v(2,2) = java.lang.Integer(5);
>> v(3,2) = java.lang.Integer(6);

>> o=[java.lang.Object();java.lang.Object()];
>> o(1) = k;
>> o(2) = v;
>> o.getClass()

ans =

class [Ljava.lang.Object;

>> types=javaArray('java.lang.Class',2) ;
>> types(1)=o.getClass();
>> types(2)=o.getClass();
>> in1 = innerclass.getConstructor(types).newInstance(o)
??? Java exception occurred:
java.lang.NoSuchMethodException: innerclassName.<init>([Ljava.lang.Object;, [Ljava.lang.Object;)

The [L in class [Ljava.lang.Object; indicates that the class is an Array of what follows immediately after the L. See also what is `[Ljava.lang.Object;?

When you do types(1)=o.getClass();types(2)=o.getClass(); , you assign to both elements of types the value "Object array". innerclass.getConstructor(types) then tries to find a constructor if innerclass that takes two object arrays as argument, and does not find one, hence the NoSuchMethodException .

Either you create a constructor public innerclassName(java.lang.Object[], java.lang.Object[]) or you change both value of types to java.lang.Object.class , which is probably what you meant to do (although without knowning what you want to do, the former may be more reasonable if you know you will pass in arrays).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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