简体   繁体   中英

java.lang.NullPointerException for NodeList

I have an exception with the following code

  public calculWeightdoc(OWLOntology onto, String xml) {

        for(OWLClass cls: onto.getClassesInSignature()){

        freqConcept(xml, cls);
        System.out.println("la taille de liste : "+list.getLength());

            if(list.getLength()!=0){   
                listConceptRetenus.put(cls, list.getLength());
            }

            else 
                {
                    listConceptRetenus.put(cls, 0);
                }


        }

   }

This is the function FreqConcept

    public void freqConcept(String xmldoc,OWLClass node){
        try {
            String filepath = xmldoc;
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);


             list = doc.getElementsByTagName(node.getIRI().getFragment());


        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }
    }

And this is the exception:

Exception in thread "main" java.lang.NullPointerException
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.nextMatchingElementAfter(DeepNodeListImpl.java:199)
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.item(DeepNodeListImpl.java:146)
at com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl.getLength(DeepNodeListImpl.java:117)
at com.onto.weight.document.calculWeightdoc.<init>(calculWeightdoc.java:59)
at com.onto.weight.document.Main_Class_une_seule_onto.main(Main_Class_une_seule_onto.java:70)

Actually, the same code works perfectly for some ontologies like people with 60 classes, but for the others with an important number of classes like Dbpedia with 1173 classes it doesn't work, I'm not sure if that is the problem or something else...
The exception is about that line list.getLength() in that function calculWeightdoc(OWLOntology onto, String xml) .

Thank you for sharing any idea that may hepl me ti fix the problem.

The list initialization happens only if the input does not fail parsing. Is there any stacktrace appearing when the list is initialised? If so, the list is left as null. To avoid the problem, check if the list is null before accessing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM