1

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;)
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76

1 Answers1

0

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).

Community
  • 1
  • 1
arne.b
  • 4,212
  • 2
  • 25
  • 44
  • Thanks very much for your reply. Greatly appreciated. Unfortunately I cannot change the public innerclassName constructor, it's a hard-coded class that I had to import. And also I can't change the value of types(1) and types (2) because: `>> types(1) = java.lang.Object.class ??? Assignment of char to Java arrays is not allowed` So I'm at a bit of a brick wall here! – tartanarmy123 Feb 25 '13 at 12:43
  • @tartanarmy123 Sorry, my bad, I never actually used Java in MATLAB, only the two separately. `java.lang.Object.class` is the java way to refer to the class object, but in MATLAB it creates a string with the class name somehow. Try creating a new single object and use in place of `o` when assigning to types, i.e. `dummy = java.lang,Object; types(:)=dummy.getClass();`. – arne.b Feb 25 '13 at 13:01
  • Thanks very much! Using the dummy actually got it going! Cheers!! – tartanarmy123 Feb 25 '13 at 13:22