MViewPane.getM(int) returns with null

ChemAxon 7c2d26e5cf

04-06-2004 16:22:36

MViewPane.getM returns null although the molecule are already set.


See the following example:
Code:



viewPane.setM(0, "mols-2d/caffeine.mol");


Molecule m = viewPane.getM(0);






The problem is that MViewPane.getM is called too early. The molecule has not been loaded yet.


MViewPane.setM(int, String) launches a new thread for loading a molecule and the getM(0) method does not wait until this thread is finished.





Thus, there is no guaranty that the molecule loading process is finished until the method returns. This method is generally used in case of loading a huge set of molecules in the same time. If you use debug option: viewPane.setDebug(2), you can see when the molecule is loaded.





Instead of setM(int, String) use setM(int, File, String) method. Using this method will cause setM to wait until molecule loading is finished.

ChemAxon 7c2d26e5cf

04-06-2004 16:25:47

This example shows a similar problem:


Code:



   viewPane.setM(0, "CN1C=NC2=C1C(=O)N(C)C(=O)N2C");


   Molecule m = viewPane.getM(0);





In this case, there are several ways to avoid this problem. For example, you can use MViewPane.setM(int, Molecule) method:


Code:



   String smiles="CN1C=NC2=C1C(=O)N(C)C(=O)N2C";


   byte[] buf=smiles.getBytes();


   MolImporter mi = new MolImporter(new ByteArrayInputStream(buf));


   try{


       Molecule mol = mi.importMol(buf);


       viewPane.setM(0,mol);


   }catch(Exception e) {System.err.println(e);}


   Molecule m = viewPane.getM(0);