setReactants -----> java.lang.NullPointerException

User 781a908cdf

16-08-2004 16:08:04

hello,





I want to do a reaction in a JSP and so I used the first example in the API concerning Reactor and when I want to set the reactants :





java.lang.NullPointerException


chemaxon.reaction.Reaction.getReactantCount(Reaction.java:294)


chemaxon.reaction.Reactor.getReactantCount(Reactor.java:620)


chemaxon.reaction.Reactor.setReactants(Reactor.java:573)


... etc





the error in my file is located at the line "reactor.setReactants(reactants);"





Here is my code :


Code:



        Reactor reactor = new Reactor();


        Standardizer std = new Standardizer(xml);


        Reaction reaction = new Reaction(nameReact,std);


        reactor.setReaction(reaction);


        MolHandler mh = new MolHandler(moltest);


        Molecule molec = new Molecule(mh.getMolecule());


        String mole = (mh.getMolecule()).toFormat("smiles");


        String[] tmp = mole.split("\\.");


         Molecule[] reactants = new Molecule[tmp.length];


        for(int i=0;i<tmp.length;i++){


                MolImporter molimp = new MolImporter();


                reactants[i] = molimp.importMol(tmp[i]);


        }


        reactor.setReactants(reactants);


        Molecule[] products = null;


        int nb=1;


        while ((products = reactor.react()) != null) {


            %><p>List <%=nb++%> of products : <br><ul><%


            for(int j=0;j<products.length;j++){


                %><%=products[j].toFormat("smiles")%><br><%


            }


        }








where xml come from the xml file attached, nameReact the ID in this file, moltest the reactants in mol format.





Could somebody help me ?


bye.

ChemAxon fb166edcbd

17-08-2004 15:23:52

The problem is that you forgot to set the reaction molecule.


This can be done by Reaction.setReaction(mol) if the reaction molecule is mapped in ChemAxon style, or by Reaction.setReaction(mol, 2) if the reaction is not or partially mapped (see http://www.chemaxon.com/jchem/doc/user/Reactor.html#mapping, but this is only available from jchem 2.3).





In the latest release there is a more comfortable way to do this: you can set the reaction directly by Reactor.setReaction(mol) and you do not need to create a Reaction object at all. In the latter case you can set the mapping style beforehand by Reactor.setMappingStyle(2) if your reaction molecule is not mapped.





Old API:





Code:






   Reactor reactor = new Reactor();   


        Reaction reaction = new Reaction();





   // use a multi-fragment reactant in the reaction


   // also map the reaction,


        // set query mode to read SMARTS


        // (second boolean parameter, available since version 2.2)


   MolHandler h = new MolHandler("[C:1].[C:6]1[C:5][C:4][C:3][C:2]1>>[C:6]1=[C:5][C:4]=[C:3][C:2]=[C:1]1", true);


   RxnMolecule rxmol = RxnMolecule.getReaction(h.getMolecule());





   // set the reaction


   reaction.setReaction(rxmol);


   reactor.setReaction(reaction);











New API:





Code:






   Reactor reactor = new Reactor();





   // set mapping style to use automapper


   reactor.setMappingStyle(2);   





   // use a multi-fragment reactant in the reaction   


        // if the mapping style is not set, the reaction should be mapped in ChemAxon style


        // set query mode to read SMARTS


   MolHandler h = new MolHandler("C.C1CCCC1>>C1=CC=CC=C1", true);


   Molecule rxmol = h.getMolecule();





   // set the reaction


   reactor.setReaction(rxmol);

















Full example JSPs are attached.








PS:


Looking at your code:





1) I guess you specified your reaction in the standardizer configuration basic.xml, but standardization is something else: it is used to bring the reactants and the reaction to a common form, e.g. aromatization, converting nitro-groups, ... - you can read more about this at: http://www.chemaxon.com/jchem/doc/user/Standardizer.html





2) If you have a multifragment molecule then you can use Molecule.convertToFrags() to convert it to its fragments instead of manipulating its SMILES representation (splitting by '.'). There may be a problem with this approach: reactant fragments may be put in a different order as expected by the reaction. You can try every possible ordering - in case of 2 reactants this means simply swapping the reactants if there is no product generated.