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

6 views (last 30 days)
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 Comment
Nikhil Sreekumar
Nikhil Sreekumar on 25 Apr 2017
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

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!