[Biojava-l] Why am I getting ClassCastExceptions

Thomas Down thomas at derkholm.net
Fri May 2 20:09:05 EDT 2003


Once upon a time, Sylvain Foisy wrote:
> Hi,
> 
> I am looking for motifs in promoter regions of genes as read from the
> massive *.gbk files from the human genome. When a hit is found in a
> region forward of a gene, I am seeking the CDS that is inside this gene
> and I want the full info on that CDS:
> 
>         while(iterY.hasNext()){
>           String laProp=(String)iterY.next();
>           String laPropVal=(String)annotY.getProperty(laProp);//***
>           System.out.println("+ \t\t"+laProp+": "+laPropVal);
>         }
>
> java.lang.ClassCastException
> 	at IndianaGene.main(IndianaGene.java:186)//The line with ***

There's no reason why every value in an Annotation object
has to be a String.  Depending on exactly how the data
got there, there could be all sorts of objects.  Keys which
can have multiple values are generally represented with List
objects.  You could try:

   while (iterY.hasNext()) {
       Object laProp = iterY.next();
       Object laPropVal = annotY.getProperty(laProp);
       System.out.println(
           "+ \t\t" + lapProp.toString() + ": " +
           laPropVal.toString()
       );
   }               

If you decide you're not interested in any non-String
data, just an a check for laPropVal instanceof String.

For some new data sources, we now have the allowed contents
of Annotation bundles represented using the AnnotationType
framework, which potentially gives applications a bit more
warning of what to expect.  Unfortunately, I don't think we
have the Genbank data model represented as AnnotationTypes.
Maybe it would be useful to do this at some point.

      Thomas.


More information about the Biojava-l mailing list