Re: Create an Object from an Array Class



Z <ztuffaha@xxxxxxxxx> writes:

I am using reflection to getmethod(methodName, Class parameters) and
invoke(object, obj[] parameters) methods from some Objects.

My problem is:

Say: I want to invoke a method that takes String[] object as parameter
(I was able to getmethod correctly). When I try to create an object
(to pass as args to invoke):

Object someObject= (Object) String[].class.newInstance();

That fails, because the array class does not have a normal
constructor. Here you try to invoke a default constructor.

(Note that the object is not always an instance of String[]... it
could be an instance of SomeOtherClass[])

I do get the following exception:


java.lang.InstantiationException: [Ljava.lang.String;

My question is:

Does the "[L" at the beginning and the ";" at the end have a meaning?

Yes. Java writes the name of and array of object type as "[L" followed
by the name of the class and ended by ";". I.e., the type
my.foo.Foo[]
has the name "[Lmy.foo.Foo;". Arrays of base types have other abbreviations,
e.g., "[I" for int[].

How can I solve this problem?

Array.newInstance(String.class, 0); // equivalent to: new String[0]

/L
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.



Relevant Pages

  • Re: Dynamic inheritance
    ... >> to invoke the development portion of the IDE to modify the tree and ... > That's not the case in Smalltalk, ... properties at run time when the constructor is invoked. ... repository, modify the tree with the existing IDE tools, and then let ...
    (comp.object)
  • Re: Calling a constructor from another
    ... instantiated is a composite of all the properties in all classes in the direct line of ascent in the subclassing tree, the usual technique is to provide a constructor for each class to handle that class' properties and invoke them in a nested fashion. ... While using the Factory pattern ... the method responsibilities they need to invoke constructors. ...
    (comp.object)
  • Re: Detecting wich method was called in a WS contructor
    ... It's true there is a pending invocation of a webmethod when the constructor ... Callers invoke ... > of Web Services, so I created a couple of standard classes in order to ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: 2 initializers in 1 constructor
    ... the dot not compiler does allow us to invoke 2 constuctors(whether the ... you have to invoke one of the constructor defined by the ... would become unreached oblect. ... When you call a sibling constructor, it calls a base constructor. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Multithread and UI again ! :)
    ... Can it be that you construct the second form on a worker thread? ... The Invoke method can also be tricky - make sure that the control on which you call Invoke was created on the main UI thread. ... MyAppContext constructor. ... frmSecond popping up to the screen but white and doesnt show anything. ...
    (microsoft.public.dotnet.framework.windowsforms)