Howto create javaArray with different derived classes of an abstract superclass?

6 次查看(过去 30 天)
Dear Community,
I am trying to create a Java Array consisting of objects that belong to the same superclass but are different classes by themselves. In my case I have the following code:
subject_term = javax.mail.search.SubjectTerm('asdasd');
sender_term = javax.mail.search.FromStringTerm('a@asd.de');
term_array = javaArray('javax.mail.search.SearchTerm', subject_term, sender_term);
combined_terms = javax.mail.search.AndTerm(term_array);
When I execute that code, the following error occurs:
Undefined function 'javaArray' for input arguments of type 'javax.mail.search.SubjectTerm'.
In addition to that, I cannot create an Array the following way:
term_array(3, 1) = javax.mail.search.SearchTerm();
Because SearchTerm is an abstract class.
Java exception occurred: java.lang.InstantiationException: javax.mail.search.SearchTerm
Is there a way to typecast a Java object in MATLAB? Or is there another method to create a java array in MATLAB?
Any help would be very appreciated!
Greetings
  1 个评论
Nikhil Sreekumar
Nikhil Sreekumar 2017-4-25
Hi Lewin,
Why is the 2nd and 3rd argument of javaArray aubject_term and sender_term? Shouldn't it be the dimensions? Also shouldn't it be javax.mailsearch.AndTerm(subject_term, sender_term)?
I ran the following code and didn't get any error:
subject_term = javax.mail.search.SubjectTerm('asdasd');
sender_term = javax.mail.search.FromStringTerm('a@asd.de');
term_array = javaArray('javax.mail.search.SearchTerm', 1, 2); % I am not a java expert, so don't know what is the use of dimension in this context
combined_terms= javax.mail.search.AndTerm(subject_term, sender_term)
Thanks
Nikhil

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by